2013-05-17 100 views
4

我試圖通過簡單地傳遞日期和時間來刪除Python Dataframe中的一行。從Python/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 01:00:00')

但我不斷收到以下錯誤信息:

exceptions.ValueError: labels [2012-01-01 01:00:00] not contained in axis 

任何幫助在任刪除行或只是刪除值將非常感激。

:-)

回答

8

它看起來就像你有實際使用時間戳,而不是字符串:

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 

假定日期時間是索引,如果不使用

df1 = df.set_index('Date_Time') 
+0

這是也許有點令人驚訝,因爲ix與字符串一起工作......''df1.ix ['2012-01-01 01:00:00']'。 –

+0

是的... ....需要把轉換在那裏...也許就此打開一個問題... – Jeff

+0

@Jeff打開[問題在這裏](https://github.com/pydata/pandas/issues/3637) –

相關問題