如何交换pandas pivot表中的列标题?

2024-10-03 02:44:21 发布

您现在位置:Python中文网/ 问答频道 /正文

从这个question开始跟进

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

                      AVG GrossProfit      AVG PMV      Loss%         Sales      
ParentAuction       Copart   IAA        Copart   IAA   Copart IAA    Copart   IAA 
Make                                                                        
Acura               112.99  NaN         -15.53   NaN   36.46  NaN      96.0  NaN

如何将列级别更改为此列格式?在

^{pr2}$

Tags: 数据make格式nan级别avgquestionsales
1条回答
网友
1楼 · 发布于 2024-10-03 02:44:21

使用^{}^{}进行排序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  

相关问题 更多 >