Home > database >  filter dataset in Pandas based on a specific datetime column condition
filter dataset in Pandas based on a specific datetime column condition

Time:11-24

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?

CodePudding user response:

can you try this:

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

CodePudding user response:

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