pandas merge dataframes by closest time

后端 未结 2 1747
渐次进展
渐次进展 2021-01-05 08:03

I\'ve got two dataframes (logs and failures), which I would like to merge so that I add in logs a column which has the value of the cl

2条回答
  •  走了就别回头了
    2021-01-05 08:28

    You can reindex with method="nearest". There may be a neater way, but using a Series with the failure logs in the index and values works:

    In [11]: failures_dt = pd.Series(failures["date"].values, failures["date"])
    
    In [12]: failures_dt.reindex(logs["date-time"], method="nearest")
    Out[12]:
    date-time
    2015-10-23 10:20:54   2015-10-23
    2015-10-22 09:51:32   2015-10-22
    2015-10-21 06:51:32   2015-10-21
    2015-10-28 16:59:32   2015-10-23
    2015-10-25 04:41:32   2015-10-23
    2015-10-24 11:50:11   2015-10-23
    dtype: datetime64[ns]
    
    In [13]: logs["nearest"] = failures_dt.reindex(logs["date-time"], method="nearest").values
    
    In [14]: logs
    Out[14]:
                date-time  var1    nearest
    0 2015-10-23 10:20:54     0 2015-10-23
    1 2015-10-22 09:51:32     1 2015-10-22
    2 2015-10-21 06:51:32     3 2015-10-21
    3 2015-10-28 16:59:32     1 2015-10-23
    4 2015-10-25 04:41:32     2 2015-10-23
    5 2015-10-24 11:50:11     4 2015-10-23
    

提交回复
热议问题