簡體   English   中英

從pandas數據框中刪除兩個時間戳之間的所有行

[英]Remove all rows between two timestamps from pandas dataframe

如何刪除從數據幀中的所有行兩個時間戳(含)之間?

我的數據框看起來像:

                   b      a      
0 2016-12-02 22:00:00  19.218519 
1 2016-12-02 23:00:00  19.171197  
2 2016-12-03 00:00:00  19.257836  
3 2016-12-03 01:00:00  19.195610  
4 2016-12-03 02:00:00  19.176413 

例如:我想從時間戳在以下日期之間的數據幀中刪除所有行:“ 2016-12-02 22:00:00”到“ 2016-12-03 00:00:00”。 因此,結果將僅包含第3行和第4行。

b列的類型為datetime64,a的類型為float。

請提出建議。

您可以過濾掉它們:

from_ts = '2016-12-02 22:00:00'
to_ts = '2016-12-03 00:00:00'
df = df[(df['b'] < from_ts) | (df['b'] > to_ts)]

將列b轉換為日期時間,然后應用蒙版

df.b = pd.to_datetime(df.b, format = '%Y-%m-%d %H:%M:%S')
df[(df.b < '2016-12-02 22:00:00') | (df.b > '2016-12-03 00:00:00')]

    b                   a
3   2016-12-03 01:00:00 19.195610
4   2016-12-03 02:00:00 19.176413
index_list= df.b[(df.b >= "2016-12-02 22:00:00") & (df.b <= "2016-12-03 00:00:00")].index.tolist()
df.drop(df.index[index_list] , inplace = True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM