我有几个用时间戳索引的 pandas 数据帧,我想将它们合并到一个数据帧中,匹配最近的时间戳。所以我想举个例子:
a =
CPU
2021-03-25 13:40:44.208 70.571797
2021-03-25 13:40:44.723 14.126870
2021-03-25 13:40:45.228 17.182844
b =
X Y
2021-03-25 13:40:44.193 45 1
2021-03-25 13:40:44.707 46 1
2021-03-25 13:40:45.216 50 2
a + b =
CPU X Y
2021-03-25 13:40:44.208 70.571797 45 1
2021-03-25 13:40:44.723 14.126870 46 1
2021-03-25 13:40:45.228 17.182844 50 2
最终 DataFrame 中的确切时间戳对我来说并不重要。
顺便说一句。有没有一种简单的方法可以将“绝对”时间戳从开始(以秒或毫秒为单位)转换为时间?所以对于这个例子:
CPU X Y
0.0 70.571797 45 1
0.5 14.126870 46 1
1.0 17.182844 50 2
最佳答案
使用merge_asof
使用direction='nearest'
:
pd.merge_asof(df1, df2, left_index=True, right_index=True, direction='nearest')
关于python - 按时间戳合并 Pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66801447/