Convert rows in pandas dataframe to columns

I want to convert strings to foll. pandas dataframe for 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 a pivot table, but this does not look like the result I want.

+1
source share
2 answers

I think you can first set_indexwith a column transition, then transpose with T, delete the column name rename_axisand the last one 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
+1
source
df = df.T

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

df = df.iloc[1:, :]
+1
source

All Articles