簡體   English   中英

如何使用熊貓按月,日,年過濾

[英]How to filter by month, day, year with Pandas

我使用以下方法創建DataFrame:

df = pandas.read_csv("data.csv", sep=';', parse_dates = 1, dayfirst = True)

然后,我得到以下結果:

                   Qty     System_created             Total
0                   2  2014-10-14 08:13:46.000         21.76  
1                   1  2014-10-14 08:13:46.000          4.16  
2                   2  2014-10-14 08:30:46.000         27.90  
3                   1  2014-10-14 08:30:46.000          4.95  
4                   1  2014-10-14 08:30:46.000          4.95  
5                   2  2014-11-05 11:15:47.000         21.76  
6                   1  2014-11-05 11:15:48.000          3.32  

但是我不知道如何按月份(或年份,日期,小時等)進行過濾。 df[df["System_created"].day]很理想。 那可能嗎?

只要您的pandas版本是0.15或更高,那么假設您的dtype已經是日期時間,則以下內容將起作用:

In [167]:

df[df.System_created.dt.day == 5]
Out[167]:
       Qty      System_created  Total
index                                
5        2 2014-11-05 11:15:47  21.76
6        1 2014-11-05 11:15:48   3.32

因此,基本上, dt屬性允許您訪問日期時間的組成部分以執行您希望過濾的比較

暫無
暫無

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

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