0

I have a multi-index dataframe (with indices Date and Company) df below:

                                Amount
Date      Company               
2019-10-01   BoA              3.924454e+09
             Starfirst        1.346442e+04
             Republic         7.006446e+06
2019-11-01   BoA              2.176354e+09
             Starfirst        1.287342e+04
             Republic         3.545446e+06

I want to convert df to the below (where the Company index is converted into columns, where Amount is the values and Date remains as an index):

                BoA            Starfirst          Republic
Date                      
2019-10-01  3.924454e+09       1.346442e+04       7.006446e+06
2019-11-01  2.176354e+09       1.287342e+04       3.545446e+06

I've been trying to do this but haven't really got very far.

Jojo
  • 1,117
  • 2
  • 15
  • 28
  • Straight-away application of `pivot_table`.. `df.reset_index().pivot_table(index='Date', columns='Company', values='Amount')` – rafaelc Oct 08 '20 at 20:18

1 Answers1

1

Use unstack to unstack the inner most index level.

df['Amount'].unstack()

Output:

Company              BoA   Republic  Starfirst
Date                                          
2019-10-01  3.924454e+09  7006446.0   13464.42
2019-11-01  2.176354e+09  3545446.0   12873.42
Scott Boston
  • 147,308
  • 15
  • 139
  • 187