简体   繁体   English

如何使用熊猫按月,日,年过滤

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

I create the DataFrame with: 我使用以下方法创建DataFrame:

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

I then get the following results: 然后,我得到以下结果:

                   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  

But I do not know how to filter by month(or year, day, hour etc...). 但是我不知道如何按月份(或年份,日期,小时等)进行过滤。 Something like df[df["System_created"].day] would be ideal. df[df["System_created"].day]很理想。 Is that possible? 那可能吗?

So long as your pandas version is 0.15 or higher then the following would work assuming your dtype is already a datetime: 只要您的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

So basically the dt attribute allows you to access the components of your datetime to perform the comparisons you desire for filtering 因此,基本上, dt属性允许您访问日期时间的组成部分以执行您希望过滤的比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM