簡體   English   中英

熊貓按日期過濾條目

[英]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.

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