How to swap the column header in pandas pivot table?

前端 未结 1 1182
旧巷少年郎
旧巷少年郎 2021-01-19 06:17

Following up from this question

I got a datframe after pivoting like this .

                      AVG GrossProfit      AVG PMV      Loss%         Sales           


        
相关标签:
1条回答
  • 2021-01-19 06:41

    Use swaplevel with sort_index for sorting MultiIndex:

    df = df.swaplevel(0,1, axis=1).sort_index(axis=1)
    print (df)
    ParentAuction           Copart                                  IAA          \
                   AVG GrossProfit AVG PMV  Loss% Sales AVG GrossProfit AVG PMV   
    Make                                                                          
    Acura                   112.99  -15.53  36.46  96.0             NaN     NaN   
    
    ParentAuction               
                   Loss% Sales  
    Make                        
    Acura            NaN   NaN  
    
    0 讨论(0)
提交回复
热议问题