Dataframe重新索引列

时间:2015-08-07 20:58:02

标签: python pandas

这是我的数据框

      ticker        date   dateValue
549      RCG  2015-01-02    10
692      RCG  2015-01-05    8

我希望这有重新索引

foo =  foo.reindex(index=['2015-01-01', '2015-01-02'])

ticker        date   dateValue
RCG  2015-01-01    N/A
RCG  2015-01-02    10

相反,我得到

       ticker date   dateValue
2015-01-01    NaN  NaN          NaN
2015-01-02    NaN  NaN          NaN

1 个答案:

答案 0 :(得分:3)

您只需设置索引:

df = pd.DataFrame({'ticker': ['RCG','RCG'], 'date': ['2015-01-02','2015-01-05'], 'dateValue':[10,8]}, index=[549, 692])
df.set_index('date', inplace=True)
print(df)

            dateValue ticker
date                        
2015-01-02         10    RCG
2015-01-05          8    RCG

这是预期的结果吗?