How to merge two data frames based on nearest date

后端 未结 2 2010
日久生厌
日久生厌 2021-01-05 19:22

I want to merge two data frames based on two columns: \"Code\" and \"Date\". It is straightforward to merge data frames based on \"Code\", however in case of \"Date\" it bec

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-05 19:52

    Here's an alternative solution:

    1. Merge on Code.

    2. Add a date difference column according to your need (I used abs in the example below) and sort the data using the new column.

    3. Group by the records of the first data frame and for each group take a record from the second data frame with the closest date.

    Code:

    df = df1.reset_index()[column_names1].merge(df2[column_names2], on='Code')
    df['DateDiff'] = (df['Date1'] - df['Date2']).abs()
    df.sort_values('DateDiff').groupby('index').first().reset_index()
    

提交回复
热议问题