如何更改 Pandas MultiIndex 列的顺序/分组/级别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29859296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:15:21  来源:igfitidea点击:

How do I change order/grouping/level of pandas MultiIndex columns?

pythonpandas

提问by Matt Savoie

I'm trying to reorder/swaplevel/pivot/something columns in a pandas dataframe. The columns are a MultiIndex, but I can't find the sauce to do what I want.

我正在尝试在 Pandas 数据框中重新排序/swaplevel/pivot/something 列。这些列是一个 MultiIndex,但我找不到做我想做的事。

The fastest varying column in my multiIndex is month, but I would like it to be the slowest varying column.

我的 multiIndex 中变化最快的列是月,但我希望它是变化最慢的列。

I've got a nbviewer notebook if you would like to try it out yourself: http://nbviewer.ipython.org/gist/flamingbear/4cfac24c80fe34a67474

如果您想自己尝试一下,我有一个 nbviewer 笔记本:http://nbviewer.ipython.org/gist/flamingbear/4cfac24c80fe34a67474

What I have:

我拥有的:

+-------------------------------------------------------------------+
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||     |weight             |extent            |rank                ||
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||month|'1Jan'|'Feb' |'Mar'|'1Jan'|'Feb'|'Mar'|'1Jan'|'Feb'|'Mar'|  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||year |      |      |     |      |     |     |      |     |     |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||2000 |45.1  |46.1  |25.1 |13.442|14.94|15.02|13    |17   |14   |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||2001 |85.0  |16.0  |49.0 |13.380|14.81|15.14|12    |15   |17   |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||2002 |90.0  |33.0  |82.0 |13.590|15.13|14.88|15    |22   |10   |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||2003 |47.0  |34.0  |78.0 |13.640|14.83|15.27|17    |16   |22   |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
+-------------------------------------------------------------------+

What I want

我想要的是

+------------------------------------------------------------------+
|+-----+------+------+----+------+------+-----+------+------+----+ |
||month|1Jan              |Feb                |Mar                ||
|+-----+------+------+----+------+------+-----+------+------+----+ |
||     |weight|extent|rank|weight|extent|rank |weight|extent|rank| |
|+-----+------+------+----+------+------+-----+------+------+----+ |
||year |      |      |    |      |      |     |      |      |    | |
|+-----+------+------+----+------+------+-----+------+------+----+ |
||2000 |45.1  |13.442|13  |46.1  |14.94 |17   | 25.1 |15.02 |14  | |
|+-----+------+------+----+------+------+-----+------+------+----+ |
||2001 |85.0  |13.380|12  |16.0  |14.81 |15   | 49.0 |15.14 |17  | |
|+-----+------+------+----+------+------+-----+------+------+----+ |
||2002 |90.0  |13.590|15  |33.0  |15.13 |22   | 82.0 |14.88 |10  | |
|+-----+------+------+----+------+------+-----+------+------+----+ |
||2003 |47.0  |13.640|17  |34.0  |14.83 |16   | 78.0 |15.27 |22  | |
|+-----+------+------+-----------+------+-----+------+------+----+ |
+------------------------------------------------------------------+

Any help would be appreciated. I can work with my original DataFrame, but writing to a CSV with the desired ordering would be fantastic.

任何帮助,将不胜感激。我可以使用我的原始 DataFrame,但是以所需的顺序写入 CSV 会很棒。

Thanks in advance, Matt

提前致谢,马特

回答by Alexander

Your columns are a MultiIndex. You need to reassign the DataFrame's columns with a new MultiIndex created from swapping levels of the existing one:

您的列是 MultiIndex。您需要使用从现有的交换级别创建的新 MultiIndex 重新分配 DataFrame 的列:

df.columns = df.columns.swaplevel(0, 1)
df.sortlevel(0, axis=1, inplace=True)
>>> df

month   '1Jan'                 'Feb'                 'Mar'              
        weight  extent  rank  weight  extent  rank  weight  extent  rank
year                                                                    
2000      45.1  13.442    13    46.1   14.94    17    25.1   15.02    14
2001      85.0  13.380    12    16.0   14.81    15    49.0   15.14    17
2002      90.0  13.590    15    33.0   15.13    22    82.0   14.88    10
2003      47.0  13.640    17    34.0   14.83    16    78.0   15.27    22

You can then export to csv:

然后您可以导出到 csv:

df.to_csv(filename)

EDIT

编辑

Per the comment from @Silas below, sortlevelhas been deprecated. Instead, use:

根据下面@Silas 的评论,sortlevel已被弃用。相反,使用:

df.sort_index(axis=1, level=0, inplace=True)

回答by Mehdi S

Since levels indices are no more mandatory you can have even more simple way to achieve the level swapping of multi-index dataframe:

由于级别索引不再是强制性的,您可以使用更简单的方法来实现多索引数据帧的级别交换:

df = df.swaplevel(axis='columns')