0

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

2
  • Could you provide a sample input with the expected output? Commented Aug 7, 2016 at 11:10
  • What exactly do you mean by row labels? The entries in the Item column, perhaps? If so, you can obtain a Series object containing the elements of that column as df['Item'], where df is your DataFrame. Commented Aug 7, 2016 at 11:15

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.