Merging Two Dataframes In Pandas Based On Time-range Difference
I have these two dataframes, df1,df2. df1: dateTime userId session 2018-08-30 02:20:19 2233 1 2018-08-30 05:32:10 1933 1 2018-08-30 09:10:39
Solution 1:
IIUC: Use pandas.merge_asof
pd.merge_asof(df1,df2,left_on='dateTime',right_on='clickTime',by='userId',direction='nearest')dateTimeuserIdsessionclickTimeclickId02018-08-30 02:20:19 2233 12018-08-30 02:21:09 198712018-08-30 05:32:10 1933 12018-08-30 05:33:10 200922018-08-30 09:10:39 2233 22018-08-30 02:32:09 199032018-08-30 10:26:59 2233 32018-08-30 02:32:09 199042018-08-30 11:56:25 4459 12018-08-30 11:57:25 301252018-08-30 12:30:55 4459 12018-08-30 11:58:55 3013
You can specify a tolerance on how far away to look
pd.merge_asof(df1,df2,left_on='dateTime',right_on='clickTime',by='userId',direction='nearest',tolerance=pd.Timedelta(15,unit='m'))dateTimeuserIdsessionclickTimeclickId02018-08-30 02:20:19 2233 12018-08-30 02:21:09 1987.012018-08-30 05:32:10 1933 12018-08-30 05:33:10 2009.022018-08-30 09:10:39 2233 2NaTNaN32018-08-30 10:26:59 2233 3NaTNaN42018-08-30 11:56:25 4459 12018-08-30 11:57:25 3012.052018-08-30 12:30:55 4459 1NaTNaN
Post a Comment for "Merging Two Dataframes In Pandas Based On Time-range Difference"