Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

python - How to filter by month, day, year with Pandas

I create the DataFrame with:

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. Is that possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

So basically the dt attribute allows you to access the components of your datetime to perform the comparisons you desire for filtering


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...