【问题标题】:get the records before and after the nearest merge by 30 minutes in python在python中获取最近合并前后30分钟的记录
【发布时间】:2020-08-10 16:43:01
【问题描述】:

我在 csv 文件中有两个数据框。第一个数据描述了交通事件(df1),第二个数据具有每 15 分钟的交通记录数据(df2)。我想根据最接近的时间在它们之间合并。我使用了 python pandas_merge_asof,我得到了最接近的匹配。但我想从交通记录数据中获得比赛前后 30 分钟的记录。我想加入最接近交通数据时间的事件。如果事件发生在 14:02:00,它将与 14:00:00 记录的交通日期合并

例如:

1- 事件数据

Date                detector_id              Inident_type
09/30/2015 8:00:00      1                      crash
09/30/2015 8:02:00      1                    congestion
04/22/2014 15:30:00     9                    congestion
04/22/2014 15:33:00     9                  Emergency vehicle 

    

2 - 流量数据

Date              detector_id               traffic_volume
09/30/2015 7:30:00      1                         55
09/30/2015 7:45:00      1                         45
09/30/2015 8:00:00      1                         60
09/30/2015 8:15:00      1                         200
09/30/2015 8:30:00      1                         70
04/22/2014 15:00:00     9                         15
04/22/2014 15:15:00     9                          7
04/22/2014 15:30:00     9                         50
04/22/2014 15:45:00     9                         11
04/22/2014 16:00:00     9                         7

2- 想要的表格

Date              detector_id               traffic_volume     Incident_type
09/30/2015 7:30:00      1                         55                  NA
09/30/2015 7:45:00      1                         45                  NA
09/30/2015 8:00:00      1                         60                Crash
09/30/2015 8:00:00      1                         60              congestion   
09/30/2015 8:15:00      1                         200                 NA
09/30/2015 8:30:00      1                         70                  NA
04/22/2014 15:00:00     9                         15                  NA
04/22/2014 15:15:00     9                          7                  NA
04/22/2014 15:30:00     9                         50              Congestion
04/22/2014 15:30:00     9                         50        Emergency vehicle   
04/22/2014 15:45:00     9                         11                  NA
04/22/2014 16:00:00     9                         7                   NA

我使用的代码如下

Merge = pd.merge_asof(df2, df1, left_index = True, right_index = True, allow_exact_maches = False,
on='Date', by='detector_id', direction='nearest')

但它给了我这张桌子。

Date              detector_id               traffic_volume     Incident_type
09/30/2015 8:00:00      1                         60                Crash
04/22/2014 15:30:00     9                         50              Congestion

我想知道事件发生前后的情况。

有什么想法吗? 谢谢。

*如果我这样问错了,请告诉我。

【问题讨论】:

    标签: pandas dataframe join merge fuzzyjoin


    【解决方案1】:

    对于任何有同样问题并想使用 pandas.merge_asof 进行合并的人,您必须使用 Tolerance 函数。此功能可帮助您调整两个数据集之间的时间差。

    但是您可能会遇到两个与 Timedelta 和排序索引相关的问题。因此 Timedelta 的解决方案是将时间转换为日期时间,如下所示:

    df1.Date = pd.to_datetime(df1.Date)
    df2.Date = pd.to_datetime(df2.Date)
    

    以及您需要在主代码中应用排序的排序索引如下:

    x = pd.merge_asof(df1.sort_values('Date'), #sort_values fix the error"left Key must be sorted"
                      df2.sort_values('Date'), 
                      on = 'Date', 
                      by = 'Detector_id',
                      direction = 'backward', 
                      tolerance =pd.Timedelta('45 min'))
    

    方向可能是最近的,在我的情况下,它将在 45 分钟内选择匹配前后的所有记录。

    方向可能是向后的,将在完全匹配或最接近匹配后 45 分钟内合并所有记录 和 Forward 将选择完全匹配或最接近匹配前 45 分钟内的所有记录。

    谢谢你,希望这对以后的任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2023-03-28
      • 1970-01-01
      • 2016-08-20
      • 2021-12-01
      • 1970-01-01
      • 2015-12-19
      • 2016-01-01
      • 1970-01-01
      相关资源
      最近更新 更多