1

I want to convert rows in the foll. pandas dataframe to column headers:

                     transition          area
0                    A_to_B       -9.339710e+10
1                    B_to_C       2.135599e+02

result:

   A_to_B            B_to_C
0  -9.339710e+10     2.135599e+02

I tried using pivot table, but that does not seem to give the result I want.

2 Answers 2

2

I think you can first set_index with column transition, then transpose by T, remove columns name by rename_axis and last reset_index:

print df.set_index('transition').T.rename_axis(None, axis=1).reset_index(drop=True)
         A_to_B    B_to_C
0 -9.339710e+10  213.5599
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @jezrael. This works great on a windows machine, but surprisingly fails on mac os x (python 2.7.11 on both and latest pandas version). On Mac OS X, I get error: TypeError: Must pass an index to rename
my bad, the pandas version on mac os x was 0.17 and not 0.18. updated version and it works now
2
df = df.T

df.columns = df.iloc[0, :]

df = df.iloc[1:, :]

1 Comment

I think you need add reset_index and rename_axis to your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.