200 400
我有一个数据帧(df1),并希望从另一个数据帧(df2)获得ID和相关分数的前最近的survey_date
df1 = pd.DataFrame({'ID' : [1,2],
'start_date':['2018-08-04','2018-08-09']})
df1
df2 = pd.DataFrame({'ID' : [1,1,2,2],
'survey_date':['2018-08-01','2018-08-05','2018-08-08','2018-08-10'],
'score':[200,100, 400, 800]})
df2
所需输出
您可以试试merge_asof
#df1.start_date = pd.to_datetime(df1.start_date)
#df2.survey_date = pd.to_datetime(df2.survey_date)
out = pd.merge_asof(df1, df2, by = 'ID', left_on = 'start_date', right_on = 'survey_date')
Out[366]:
ID start_date survey_date score
0 1 2018-08-04 2018-08-01 200
1 2 2018-08-09 2018-08-08 400