gpt4 book ai didi

python - 如何交换 pandas 数据透视表中的列标题?

转载 作者:太空宇宙 更新时间:2023-11-04 00:30:04 25 4
gpt4 key购买 nike

从此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

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

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

最佳答案

使用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

关于python - 如何交换 pandas 数据透视表中的列标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46168316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com