![](/img/trans.png)
[英]Filter entries of pandas dataframe based on presence (absence) of entries older than a date
[英]pandas filter entries by date
有一個名為“ date”的鍵的數據框。 前幾個條目如下所示:
0 02.01.2013
1 03.01.2013
2 05.01.2013
3 06.01.2013
4 15.01.2013
現在,我想使用熊貓來過濾掉所有非2014年日期的行。
我瀏覽了教程,發現以下內容:
mask = transactions['date'][9]==4
trans=transactions[mask]
但這不起作用
transactions['date'][9]
給我第9個數據條目,但沒有第9位數字。
有人可以幫助新手嗎?
df
date
0 02.01.2013
1 03.01.2013
2 05.01.2013
3 06.01.2014
4 15.01.2014
使用pd.to_datetime
將列轉換為datetime
,並測試dt.year
屬性-
m = pd.to_datetime(df.date).dt.year != 2014
m
0 True
1 True
2 True
3 False
4 False
Name: date, dtype: bool
使用遮罩對df
進行過濾-
df = df[m]
如果datetime列是索引,則需要轉換df.index
m = pd.to_datetime(df.index).year != 2014
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.