问题
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,
回答1:
df.reset_index(level=0, inplace=True)
should do it. See the docs here.
来源:https://stackoverflow.com/questions/20228998/make-column-from-pandas-dataframe-index