如何交换熊猫数据透视表中的列标题?

Flo*_*oor 4 python pivot-table levels pandas

从这个问题跟进

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

                      AVG GrossProfit AVG PMV Loss% Sales      
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

jez*_*ael 5

使用swaplevelsort_index排序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  
Run Code Online (Sandbox Code Playgroud)