如何使用 pandas 按月,日,年过滤 [英] How to filter by month, day, year with Pandas
问题描述
我使用以下方法创建DataFrame:
I create the DataFrame with:
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]
这样的东西将是理想的.有可能吗?
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. Is that possible?
推荐答案
只要您的熊猫版本为0.15
或更高版本,那么假设您的dtype
已经是日期时间,以下操作将起作用:
So long as your pandas version is 0.15
or higher then the following would work assuming your dtype
is already a datetime:
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
属性允许您访问日期时间的组成部分以执行您希望过滤的比较
So basically the dt
attribute allows you to access the components of your datetime to perform the comparisons you desire for filtering
这篇关于如何使用 pandas 按月,日,年过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!