8-Bit Borges
8-Bit Borges

Reputation: 10033

Turn pivot table into regular dataframe

How can I get this pivot table:

                        mean
                   points_num
round     position            
1         FWD        2.445283
          MID        1.628571
          DEF        2.378571
2         FWD        3.000000
          MID        2.651351
          DEF        1.930435

df.columns:

[108 rows x 1 columns]
MultiIndex([('mean', 'points_num')],
           )

And change it into a dataframe like so:

Round   FWD       MID        DEF
1       2.445283  1.628571  2.378571
2       3.000000  2.651351  1.930435

Upvotes: 0

Views: 21

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Can you try:

pivot_df.iloc[:,0].unstack('position')

However, you can probably be better off using pivot_table on the original dataframe:

df.pivot_table(index='round', column='position', 
               values='points_num', aggfunc='mean'
              )

Upvotes: 1

Related Questions