filter dataset in Pandas based on a specific datetime column condition

Question:

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?

Asked By: Avneesh Chaudhary

||

Answers:

can you try this:

New_df =df1.loc[abs((df1['record_date'] - df1['event_date'])).dt.days <= 3] #get +- 3 days
Answered By: Clegane
new_df = df.loc[df.record_date.sub(df.event_date).abs().le('3d')]
Answered By: BeRT2me
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.