Python:如何基于时间戳合并两个数据帧?

I have two dataframes df1 and df2. df1 contains information of indexes of df2

df1
    i   j   val
0   0   83  23.728399
1   0   168 1.275909
2   0   171 18.523408
3   0   362 16.381101
4   0   363 17.699709

where i and j corresponds to idx of the column in df2.

df2.head(5)
         t_start           t_end   idx  ID
0   1.584730e+09    1.584748e+09    0   A
1   1.584732e+09    1.584749e+09    1   B
2   1.584664e+09    1.584708e+09    2   C
3   1.584670e+09    1.584745e+09    3   D
4   1.584706e+09    1.584723e+09    4   E

I want to keep the values in df1 when there is an overlap in the time between the t_start and t_end

df2['t_start_date'] = pd.to_datetime(df2['t_start'],unit='s')
df2['t_end_date'] = pd.to_datetime(df2['t_end'],unit='s')

If I look at the the index i=0 and j=83

print(df2['t_start_date'][0], ",", df2['t_end_date'][0], " --- ", df2['t_start_date'][83], ",", df2['t_end_date'][83])


2020-03-20 18:50:46 , 2020-03-20 23:51:34  ---  2020-03-20 07:52:56 , 2020-03-20 22:59:33

In this case, I would keep the first row of df1 because there is an intersection between the two timestaps.

评论