问题描述
我正在尝试对pandas数据框中的列进行重新排序/交换级别/数据透视表/某些列.这些列是一个MultiIndex,但我找不到满足自己需求的调味酱.
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.
multiIndex中变化最快的列是month,但我希望它是变化最快的列.
The fastest varying column in my multiIndex is month, but I would like it to be the slowest varying column.
如果您想自己尝试一下,我有一个nbviewer笔记本: http://nbviewer.ipython.org/gist/flamingbear/4cfac24c80fe34a67474
I've got a nbviewer notebook if you would like to try it out yourself: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会很棒.
Any help would be appreciated. I can work with my original DataFrame, but writing to a CSV with the desired ordering would be fantastic.
预先感谢,马特
推荐答案
您的列是一个MultiIndex.您需要使用通过交换现有索引级别创建的新MultiIndex重新分配DataFrame的列:
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:
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
然后您可以导出到csv:
You can then export to csv:
df.to_csv(filename)
编辑
根据下面@Silas的评论,sortlevel
已被弃用.而是使用:
Per the comment from @Silas below, sortlevel
has been deprecated. Instead, use:
df.sort_index(axis=1, level=0, inplace=True)
这篇关于如何更改 pandas MultiIndex列的顺序/分组/级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!