-1

I need to filter a pandas dataframe such that the posted_at (datetime) column value is within the last 200 days and the owner column value is a specific value that I give. I need another dataframe as the result of this query. How to achieve this?

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – secelite Nov 29 '16 at 11:42

1 Answers1

1

You can use boolean indexing with date substract by Timedelta:

start = pd.to_datetime('2016-11-23 15:00:10')
rng = pd.date_range(start, periods=10)

df = pd.DataFrame({'posted_at': rng, 'owner': ['a'] * 5 + ['b'] * 5})  
print (df)
  owner           posted_at
0     a 2016-11-23 15:00:10
1     a 2016-11-24 15:00:10
2     a 2016-11-25 15:00:10
3     a 2016-11-26 15:00:10
4     a 2016-11-27 15:00:10
5     b 2016-11-28 15:00:10
6     b 2016-11-29 15:00:10
7     b 2016-11-30 15:00:10
8     b 2016-12-01 15:00:10
9     b 2016-12-02 15:00:10
now = pd.datetime.now().date()
print (now)
2016-11-29

#in real data change 5 to 200
last5 =  now - pd.Timedelta('5D')

#another solution
#last5 =  now - pd.offsets.Day(5)
print (last5)
2016-11-24

mask = (df.owner == 'a') & (df.posted_at > last5) & (df.posted_at < now)
print (mask)
0    False
1     True
2     True
3     True
4     True
5    False
6    False
7    False
8    False
9    False
dtype: bool

print (df[mask])
  owner           posted_at
1     a 2016-11-24 15:00:10
2     a 2016-11-25 15:00:10
3     a 2016-11-26 15:00:10
4     a 2016-11-27 15:00:10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks. I was working in this direction only. This was exactly what i was looking for. – sarthak sahni Nov 29 '16 at 12:06
  • thank you for accepting. Small advice - [How to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) - then get no downvotes and get many nice answers. Nice day! – jezrael Nov 29 '16 at 12:14