将大 pandas 多索引转成列 [英] Turn Pandas Multi-Index into column
本文介绍了将大 pandas 多索引转成列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含2个索引级别的数据框:
I have a dataframe with 2 index levels:
value
Trial measurement
1 0 13
1 3
2 4
2 0 NaN
1 12
3 0 34
我想转换成这样:
Trial measurement value
1 0 13
1 1 3
1 2 4
2 0 NaN
2 1 12
3 0 34
我最好怎么做?
我需要这个,因为我想汇总数据按照这里指示
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.
推荐答案
reset_index()是一个Pandas DataFrame方法,它将索引值作为列传输到DataFrame中。参数的默认设置为 drop = False (将索引值保留为列)
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(inplace = True)
在DataFrame的名称之后:
All you have to do add .reset_index(inplace=True)
after the name of the DataFrame:
df.reset_index(inplace=True)
这篇关于将大 pandas 多索引转成列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文