I want date new column (Output) from df2['Date']
.
Condition: vlookup DEVICE_SN and first over date from df2['Date']
.
Take simple dataframe by dates.
I want date new column (Output) from df2['Date']
.
Condition: vlookup DEVICE_SN and first over date from df2['Date']
.
Take simple dataframe by dates.
Here I have used two lambda first to get the list of greater than dates of each ID, in next lambda I am selecting first dates of each list, in case if will empty list will return 'no'
Code:
#Change column type str to date, so you can Compare
df1['Date']= pd.to_datetime(df1['Date'])
df2['Date']= pd.to_datetime(df2['Date'])
df1['output'] = df1.apply(lambda r: [d for d in df2.loc[df2['ID']==r.ID]['Date'] if d > r.Date], axis=1).apply(lambda x: x[0] if x else 'no')
df1
Output:
ID Date output
0 11 2022-05-27 2022-06-11 00:00:00
1 22 2022-01-12 no
2 33 2022-03-01 2022-06-28 00:00:00
3 44 2018-07-19 no
4 55 2018-10-11 no