Python-on-Date索引

2024-04-23 14:34:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试根据日期分割数据帧,这是索引。我的数据如下:

  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

Tags: 数据代码dfdateindextimevalue错误
1条回答
网友
1楼 · 发布于 2024-04-23 14:34:01

您需要^{}

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

如果^{}中的值已排序,请使用^{}

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

相关问题 更多 >