1 回答
TA贡献1805条经验 获得超10个赞
一个想法是过滤DataFrame
每行并删除原始行DataFrame.drop
:
td = pd.Timedelta("1m")
f = lambda x, y: df.loc[df['event_time'].between(y - td, y + td),
'event_id'].drop(x).tolist()
df['related_event_id_list'] = [f(k, v) for k, v in df['event_time'].items()]
print (df)
event_time event_id related_event_id_list
0 2020-01-01 10:00:00 1 [2]
1 2020-01-01 10:01:00 2 [1, 3a, 3b]
2 2020-01-01 10:02:00 3a [2, 3b]
3 2020-01-01 10:02:00 3b [2, 3a]
4 2020-01-01 10:30:00 4 []
5 2020-01-01 10:50:00 5 []
6 2020-01-01 10:54:00 6 [7]
7 2020-01-01 10:55:00 7 [6]
添加回答
举报