date1 | date2 |
---|---|
13/08/2021 | 21/08/2021 |
In the above python dataframe, I have 2 columns date1 and date2. Need to check whether the date in column date1 is within +- 10days of the date in column date2. If yes it should return true, else false. In this case, it should return true.
You can use the .dt.days
method to find the difference between dates.
import pandas as pd
data = {
"date1": ['13/08/2021','19/08/2021','13/08/2021','23/08/2021','30/08/2021'],
"date2": ['21/08/2021','30/08/2021','23/08/2021','14/08/2021','13/08/2021']
}
df = pd.DataFrame(data)
df[['date1','date2']] = df[['date1','date2']].apply(pd.to_datetime)
df['days'] = (df['date2'] - df['date1']).dt.days
df['Result'] = abs((df['date2'] - df['date1']).dt.days) <=10
print(df)
[ Output ]
date1 date2 days Result
0 2021-08-13 2021-08-21 8 True
1 2021-08-19 2021-08-30 11 False
2 2021-08-13 2021-08-23 10 True
3 2021-08-23 2021-08-14 -9 True
4 2021-08-30 2021-08-13 -17 False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.