如何将熊猫数据索引转换成列?这似乎很明显,但我似乎不知道如何将数据帧索引转换为列?例如:df=
gi ptt_loc 0 384444683 593
1 384444684 594
2 384444686 596到,df=
index1 gi ptt_loc 0 0 384444683 593
1 1 384444684 594
2 2 384444686 596
3 回答

森林海
TA贡献2011条经验 获得超2个赞
要么:
df['index1'] = df.index
或,.reset_index:
df.reset_index(level=0, inplace=True)
因此,如果您有一个具有3个索引级别的多索引框架,如:
>>> df
val
tick tag obs
2016-02-26 C 2 0.0139
2016-02-27 A 2 0.5577
2016-02-28 C 6 0.0303
你想把第一个(tick)和第3(obs)在索引列中的级别,您可以这样做:
>>> df.reset_index(level=['tick', 'obs'])
tick obs val
tag
C 2016-02-26 2 0.0139
A 2016-02-27 2 0.5577
C 2016-02-28 6 0.0303

慕田峪7331174
TA贡献1828条经验 获得超13个赞
df['si_name'] = R.index.get_level_values('si_name')
si_name
添加回答
举报
0/150
提交
取消