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?
Answers:
can you try this:
New_df =df1.loc[abs((df1['record_date'] - df1['event_date'])).dt.days <= 3] #get +- 3 days
new_df = df.loc[df.record_date.sub(df.event_date).abs().le('3d')]
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?
can you try this:
New_df =df1.loc[abs((df1['record_date'] - df1['event_date'])).dt.days <= 3] #get +- 3 days
new_df = df.loc[df.record_date.sub(df.event_date).abs().le('3d')]