Python 将 Pandas Multi-Index 转成列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20110170/
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
Turn Pandas Multi-Index into column
提问by TheChymera
I have a dataframe with 2 index levels:
我有一个具有 2 个索引级别的数据框:
value
Trial measurement
1 0 13
1 3
2 4
2 0 NaN
1 12
3 0 34
Which I want to turn into this:
我想变成这个:
Trial measurement value
1 0 13
1 1 3
1 2 4
2 0 NaN
2 1 12
3 0 34
How can I best do this?
我怎样才能最好地做到这一点?
I need this because I want to aggregate the data as instructed here, but I can't select my columns like that if they are in use as indices.
我需要这个,因为我想按照此处的说明聚合数据,但如果它们用作索引,我无法选择这样的列。
采纳答案by CraigSF
The reset_index()is a pandas DataFrame method that will transfer index values into the DataFrame as columns. The default setting for the parameter is drop=False(which will keep the index values as columns).
所述reset_index()是一个数据帧熊猫方法,将索引值转移到数据帧为列。该参数的默认设置是drop=False(它将索引值保留为列)。
All you have to do add .reset_index(inplace=True)after the name of the DataFrame:
您只需.reset_index(inplace=True)在 DataFrame 名称后添加:
df.reset_index(inplace=True)
回答by Karl Anka
This doesn't really apply to your case but could be helpful for others (like myself 5 minutes ago) to know. If one's multindex have the same name like this:
这并不真正适用于您的情况,但可能对其他人(如 5 分钟前的我)了解有所帮助。如果一个人的多重索引有这样的同名:
value
Trial Trial
1 0 13
1 3
2 4
2 0 NaN
1 12
3 0 34
df.reset_index(inplace=True)will fail, cause the columns that are created cannot have the same names.
df.reset_index(inplace=True)将失败,导致创建的列不能具有相同的名称。
So then you need to rename the multindex with df.index = df.index.set_names(['Trial', 'measurement'])to get:
那么你需要重命名多重索引df.index = df.index.set_names(['Trial', 'measurement'])以获得:
value
Trial measurement
1 0 13
1 1 3
1 2 4
2 0 NaN
2 1 12
3 0 34
And then df.reset_index(inplace=True)will work like a charm.
然后df.reset_index(inplace=True)会像魅力一样工作。
I encountered this problem after grouping by year and month on a datetime-column(not index) called live_date, which meant that both year and month were named live_date.
我在名为 的日期时间列(不是索引)上按年和月分组后遇到了这个问题live_date,这意味着年和月都被命名为live_date。
回答by sameagol
As @cs95 mentioned in a comment, to drop only one level, use:
正如@cs95 在评论中提到的,要只降低一个级别,请使用:
df.reset_index(level=[...])
df.reset_index(level=[...])
This avoids having to redefine your desired index after reset.
这样可以避免在重置后重新定义所需的索引。

