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

k2arahey  于 2023-01-15  发布在  其他
关注(0)|答案(3)|浏览(183)

我正在尝试重新排序panda Dataframe 中的/swaplevel/pivot/something列。这些列是多索引,但我找不到所需的调味料。
我的multiIndex中变化最快的列是month,但我希望它是变化最慢的列。
我有一个nbviewer笔记本,如果你想尝试一下自己:http://nbviewer.ipython.org/gist/flamingbear/4cfac24c80fe34a67474
我拥有的:

+-------------------------------------------------------------------+
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
||     |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   |  |
|+-----+------+------+-----+------+-----+-----+------+-----+-----+  |
+-------------------------------------------------------------------+

我想要的

+------------------------------------------------------------------+
|+-----+------+------+----+------+------+-----+------+------+----+ |
||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  | |
|+-----+------+------+-----------+------+-----+------+------+----+ |
+------------------------------------------------------------------+

任何帮助都将不胜感激。我可以使用我原来的dataframe,但是用想要的顺序写到CSV将是非常棒的。
先谢了马特

e0uiprwp

e0uiprwp1#

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

df.columns = df.columns.swaplevel(0, 1)
df.sort_index(axis=1, level=0, 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

然后可以导出为csv:

df.to_csv(filename)
sd2nnvve

sd2nnvve2#

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

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

nsc4cvqm3#

另一个不需要显式索引排序的方法是

df.stack(0).unstack()

相关问题