I'm trying to merge two Pandas dataframes on two columns. One column has a unique identifier that could be used to simply .merge()
the two dataframes. However, the second column merge would actually use .merge_asof()
because it would need to find the closest date, not an exact date match.
There is a similar question here: Pandas Merge on Name and Closest Date, but it was asked and answered nearly three years ago, and merge_asof()
is a much newer addition.
I asked a similar here question a couple months ago, but the solution only needed to use merge_asof()
without any exact matches required.
In the interest of including some code, it would look something like this:
df = pd.merge_asof(df1, df2, left_on=['ID','date_time'], right_on=['ID','date_time'])
where the ID
's will match exactly, but the date_time
's will be "near matches".
Any help is greatly appreciated.