My question is an update of the following question link, I have the same problem but I need to merge more than 2 dataframes based on the nearest timestamp. I tried different methods but I wasn't able to find the solution yet. Thanks.
df1
time | Value1 |
---|---|
2020-07-17 14:25:03.535906075 | 108 |
2020-07-17 14:25:05.457247019 | 110 |
2020-07-17 14:25:07.467777014 | 126 |
df2
time | Value1 |
---|---|
2020-07-17 14:25:03.535018921 | 222 |
2020-07-17 14:25:04.545104980 | 150 |
2020-07-17 14:25:07.476825953 | 60 |
df3
time | Value3 |
---|---|
2020-07-17 14:25:03.535018921 | 47 |
2020-07-17 14:25:04.545104980 | 60 |
2020-07-17 14:25:08.476825953 | 120 |
Expected result
time | Value1 | Value2 | Value3 |
---|---|---|---|
2020-07-17 14:25:03.535018921 | 108 | 220 | 47 |
2020-07-17 14:25:04.545104980 | Nan | 150 | 60 |
2020-07-17 14:25:07.476825953 | 110 | NaN | NAN |
2020-07-17 14:25:07.476825953 | 126 | 60 | 120 |
I found this method in the previous question but I don't know how to apply to more than 2 dataframes:
df3 = pd.merge_asof(df1, df2, left_index=True, right_index=True, direction='nearest', tolerance=pd.Timedelta('0.3s'))
df4 = pd.merge_asof(df2, df1, left_index=True, right_index=True, direction='nearest', tolerance=pd.Timedelta('0.3s'))
df5 = df3.append(df4[df4['Value1'].isnull()]).sort_index()