【问题标题】:How to swap the column header in pandas pivot table?如何交换熊猫数据透视表中的列标题?
【发布时间】:2017-09-12 05:08:08
【问题描述】:

从此question跟进

像这样旋转后我得到了一个数据框。

AVG GrossProfit AVG PMV Loss% 销售额 ParentAuction Copart IAA Copart IAA Copart IAA Copart IAA 制作 讴歌 112.99 NaN -15.53 NaN 36.46 NaN 96.0 NaN

如何将列级别更改为这种格式的列?

ParentAuction Copart IAA AVG GrossProfit AVG PMV 损失百分比销售额 AVG GrossProfit AVG PMV 损失百分比销售额 制作 讴歌 112.99 -15.53 36.46 96.0 NaN NaN NaN NaN

【问题讨论】:

    标签: python pandas pivot-table levels


    【解决方案1】:

    使用swaplevelsort_indexMultiIndex 进行排序:

    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  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 2021-02-28
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多