How to join two dataframes for which column values are within a certain range?

后端 未结 5 866
清酒与你
清酒与你 2020-11-22 07:53

Given two dataframes df_1 and df_2, how to join them such that datetime column df_1 is in between start and end

5条回答
  •  庸人自扰
    2020-11-22 08:51

    One simple solution is create interval index from start and end setting closed = both then use get_loc to get the event i.e (Hope all the date times are in timestamps dtype )

    df_2.index = pd.IntervalIndex.from_arrays(df_2['start'],df_2['end'],closed='both')
    df_1['event'] = df_1['timestamp'].apply(lambda x : df_2.iloc[df_2.index.get_loc(x)]['event'])
    

    Output :

                timestamp         A         B event
    0 2016-05-14 10:54:33  0.020228  0.026572    E1
    1 2016-05-14 10:54:34  0.057780  0.175499    E2
    2 2016-05-14 10:54:35  0.098808  0.620986    E2
    3 2016-05-14 10:54:36  0.158789  1.014819    E2
    4 2016-05-14 10:54:39  0.038129  2.384590    E3
    

提交回复
热议问题