jake wong jake wong - 3 months ago 8
Python Question

How to set pandas Dataframe column name as headers?

I created a Dataframe with the below code.

>>>in: df_final = pandas.DataFrame(combined_data, columns=['Item', aa, bb, cc, dd])

>>>out:
Item FY2012 FY2013 FY2014 FY2015
0 Total Revenue 654.766 535.79 321.394 445.241
1 Gross Profit 256.776 268.412 156.47 220.687
2 Net Income 60.994 44.026 57.469 41.273
3 EBITDA 111.324 110.268 (41.478) 83.382


However, when I try to transpose the code by adding a
.T
, I get:

>>>in: df_final = pandas.DataFrame(combined_data, columns=['Item', aa, bb, cc, dd]).T

>>>>out:
0 1 2 3
Item Total Revenue Gross Profit Net Income EBITDA
FY2012 654.766 256.776 60.994 111.324
FY2013 535.79 268.412 44.026 110.268
FY2014 321.394 156.47 57.469 (41.478)
FY2015 445.241 220.687 41.273 83.382


After
Transposing
, what should I do so that instead of having
[0, 1, 2, 3]
as the headers, I make
Total Revenue Gross Profit Net Income EBITDA
as the headers instead?

IE: If I did not
Transpose
the Dataframe,
print(df.columns.values)
would give me
Item FY2012 FY2013 FY2014 FY2015
as the headers. But after
Transposing
the Dataframe,
[0, 1, 2, 3]
became the headers, instead of
Total Revenue Gross Profit Net Income EBITDA

Answer

You need to set the Item column as index so it becomes 'columns' when you transpose it:

df.set_index('Item').T
Out: 
Item    Total Revenue  Gross Profit  Net Income   EBITDA
FY2012        654.766       256.776      60.994  111.324
FY2013        535.790       268.412      44.026  110.268
FY2014        321.394       156.470      57.469  -41.478
FY2015        445.241       220.687      41.273   83.382
Comments