我看过很多关于扁平化多级索引的答案。我只想删除这个多索引数据框左侧的第一个索引列。
由此:
Attributes Date Close Close
Symbols AMZN ^DJI
0 2020-12-01 3220.08 29823.92
1 2020-11-30 3168.04 29638.64
2 2020-11-27 3195.34 29910.37
3 2020-11-25 3185.07 29872.47
我正在寻找这个结果:
Date Close Close
AMZN ^DJI
2020-12-01 3220.08 29823.92
2020-11-30 3168.04 29638.64
2020-11-27 3195.34 29910.37
2020-11-25 3185.07 29872.47
这可能吗?
最佳答案
如果需要将第一个 MultiIndex 列设置为索引,请使用 DataFrame.set_index
将索引和列名称重命名为 DataFrame.rename_axis
- 所以输出是 DataFrame
和 DatetimeIndex
:
df = df.set_index([('Date', '')]).rename_axis(index=None, columns=('Date',''))
print (df)
Date Close
AMZN ^DJI
2020-12-01 3220.08 29823.92
2020-11-30 3168.04 29638.64
2020-11-27 3195.34 29910.37
2020-11-25 3185.07 29872.47
关于python - 如何从多索引数据框中删除索引列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65102992/