2 回答
TA贡献1829条经验 获得超7个赞
merge_asof
与参数一起使用direction='nearest'
,也首先对列进行排序并通过以下方式创建新列DataFrame.assign
:
df1['DATETIME'] = pd.to_datetime(df1['DATETIME'])
df2['DATETIME'] = pd.to_datetime(df2['DATETIME'])
df1 = df1.sort_values('DATETIME')
df2 = df2.sort_values('DATETIME')
df = pd.merge_asof(df1,
df2[['DATETIME','ID']].assign(Restore_DT = df2['DATETIME']),
on='DATETIME',
by='ID',
direction='nearest')
print (df)
DATETIME ID State Restore_DT
0 2020-07-29 00:15:10 A Down 2020-07-29 00:19:40
1 2020-07-29 00:25:10 B Down 2020-07-29 00:35:10
2 2020-07-29 01:14:30 A Down 2020-07-29 01:40:30
3 2020-07-29 02:15:50 A Down 2020-07-29 02:18:50
TA贡献1842条经验 获得超21个赞
您可以使用merge
.
首先合并 df(这会将 datetime 与最近的 datetime1 合并)
df_cross_merge = df1.merge(df2, on='ID', how='left').query('DATETIME <= DATETIME1')
并获得您想要的 df(仅保留第一个日期时间)
df_cross_merge =df_cross_merge.drop_duplicates(['DATETIME'],keep='first')
添加回答
举报