Chaudhary
Chaudhary

Reputation: 57

filter dataset in Pandas based on a specific datetime column condition

I have a dataframe with recorded dates and event dates, I use the following script to create a new dataframe with only rows where record and event dates match.

New_df =df1.loc[(df1['record_date'] == df1['event_date'])]

However I want to include rows from dataset where record dates are +- 1 day, 2 day, 3 days from event date including above code. How can do that?

Upvotes: 0

Views: 95

Answers (2)

BeRT2me
BeRT2me

Reputation: 13242

new_df = df.loc[df.record_date.sub(df.event_date).abs().le('3d')]

Upvotes: 1

Bushmaster
Bushmaster

Reputation: 4608

can you try this:

New_df =df1.loc[abs((df1['record_date'] - df1['event_date'])).dt.days <= 3] #get +- 3 days

Upvotes: 1

Related Questions