使用set_index或reindex索引大熊猫数据框并未按预期工作。我错过了什么吗?谢谢您的帮助。请参阅下面的示例。使用a.set_index(['Date'])获得相同的结果
print(a)
a.reindex(['Date'])
print(a.index)
Adj_Close Close Date High Low Open Symbol \
3 53.015032 54.799999 2016-01-04 54.799999 53.389999 54.320000 MSFT
2 53.256889 55.049999 2016-01-05 55.389999 54.540001 54.930000 MSFT
1 52.289462 54.049999 2016-01-06 54.400002 53.639999 54.320000 MSFT
0 50.470697 52.169998 2016-01-07 53.490002 52.070000 52.700001 MSFT
Volume price_gap target
3 53778000 NaN 0.479999
2 34079700 0.130001 0.119999
1 39518900 -0.729999 -0.270001
0 56564900 -1.349998 -0.530003
答案 0 :(得分:1)
您应该使用:
a.set_index(['Date'], inplace=True)
如此处所列:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html
或者,如果您想要一个新的数据框:
b = a.set_index(['Date'])