Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

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

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.

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

I've got a nbviewer notebook if you would like to try it out yourself: 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.

Thanks in advance, Matt

question from:https://stackoverflow.com/questions/29859296/how-do-i-change-order-grouping-level-of-pandas-multiindex-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You can then export to csv:

df.to_csv(filename)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...