Pandas 从时间索引数据帧中删除行

Pandas 从时间索引数据帧中删除行,pandas,time-series,delete-row,dataframe,Pandas,Time Series,Delete Row,Dataframe,我试图通过简单地传递日期和时间来删除Pandas数据帧中的一行 数据帧具有以下结构: Date_Time Price1 Price2 Price3 2012-01-01 00:00:00 63.05 41.40 68.14 2012-01-01 01:00:00 68.20 42.44 59.64 2012-01-01 02:00:00 61.68 43.18 4

我试图通过简单地传递日期和时间来删除Pandas数据帧中的一行

数据帧具有以下结构:

Date_Time             Price1   Price2    Price3                       
2012-01-01 00:00:00    63.05    41.40    68.14
2012-01-01 01:00:00    68.20    42.44    59.64
2012-01-01 02:00:00    61.68    43.18    49.81
我一直在尝试使用
df=df.drop('2012-01-01:00:00')

但我一直收到以下错误消息:

exceptions.ValueError: labels [2012-01-01 01:00:00] not contained in axis
任何关于删除行或仅删除值的帮助都将不胜感激


:-)

看起来您必须实际使用时间戳而不是字符串:

In [11]: df1
Out[11]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 01:00:00   68.20   42.44   59.64
2012-01-01 02:00:00   61.68   43.18   49.81

In [12]: df1.drop(pd.Timestamp('2012-01-01 01:00:00'))
Out[12]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 02:00:00   61.68   43.18   49.81
假设DateTime是索引,如果不使用

df1 = df.set_index('Date_Time')

或者,这也适用于:

df1.drop(df1.loc[df1['Date_Time'] == '2012-01-01 01:00:00'].index, inplace=True)
当您希望根据datetime索引删除一系列观察值时,它也很方便。例如,2012-01-01:00:00之后的所有观察结果:

df1.drop(df1.loc[df1['Date_Time'] > '2012-01-01 01:00:00'].index, inplace=True)

这可能有点令人惊讶,因为ix使用字符串
df1.ix['2012-01-01:00:00']
问题被删除,需要
df['rev_dis']=np.where((df['date'>='2020-01-02')&(df['date']Jezrael!非常感谢!我在这件事上强调得太多了。非常感谢你的帮助。我只需保持索引的原样(不要将日期设置为索引),它就行了!