Python Pandas哪里有日期索引

时间:2017-04-21 12:16:01

标签: python pandas

我试图根据日期(即索引)拆分数据框。我的数据如下:

  print(df.head())

  date_time             value   anomaly                         
  2014-11-23 00:00:00   0.414183    0   
  2014-11-23 01:00:00   0.526574    0
  2014-11-23 02:00:00   0.734324    1

我的代码到目前为止:

 df_split = df.where(df.index >= '2014-11-23 01:00:00')

我想要的输出是:

  2014-11-23 01:00:00   0.526574    0
  2014-11-23 02:00:00   0.734324    1

我得到的错误是:

  ValueError: Array conditional must be same shape as self

1 个答案:

答案 0 :(得分:1)

您需要boolean indexing

df_split = df[df.index >= '2014-11-23 01:00:00']
print (df_split)
                        value  anomaly
date_time                             
2014-11-23 01:00:00  0.526574        0
2014-11-23 02:00:00  0.734324        1

如果DatetimeIndex中的值已排序,请使用loc

df_split = df.loc['2014-11-23 01:00:00':]
print (df_split)
                        value  anomaly
date_time                             
2014-11-23 01:00:00  0.526574        0
2014-11-23 02:00:00  0.734324        1
df_split = df['2014-11-23 01:00:00':]
print (df_split)
                        value  anomaly
date_time                             
2014-11-23 01:00:00  0.526574        0
2014-11-23 02:00:00  0.734324        1