从大 pandas 数据框索引中创建列 [英] Make column from pandas dataframe index
本文介绍了从大 pandas 数据框索引中创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
col1
CoI
AK 0 1
1 31
2 NaN
BB 0 5
1 31
2 NaN
我想把它变成这样:
col1 CoI
0 1 AK
1 31 AK
2 NaN AK
0 5 BB
1 31 BB
2 NaN BB
我该怎么做最好?我认为这是一个相当基本的功能,但是与许多其他基本熊猫事情一样,我无法在任何地方找到信息。
许多谢谢,
解决方案
df.reset_index(level = 0,inplace = True)
应该这样做。请参阅文档此处。
I have a dataframe where I would like to turn the data in the (first level of the) index into a column. Practically my df looks like this:
col1
CoI
AK 0 1
1 31
2 NaN
BB 0 5
1 31
2 NaN
And I would like to turn it into this:
col1 CoI
0 1 AK
1 31 AK
2 NaN AK
0 5 BB
1 31 BB
2 NaN BB
How can I best do this? I think this is a rather basic functionality, but as with many other "basic" pandas things I cannot find info on this anywhere.
Many Thanks,
解决方案
df.reset_index(level=0, inplace=True)
should do it. See the docs here.
这篇关于从大 pandas 数据框索引中创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文