我有一个(在这里非常简化的)pandas数据框,它看起来像这样:
df
datetime user type msg
0 2012-11-11 15:41:08 u1 txt hello world
1 2012-11-11 15:41:11 u2 txt hello world
2 2012-11-21 17:00:08 u3 txt hello world
3 2012-11-22 18:08:35 u4 txt hello you
4 2012-11-22 18:08:37 u5 txt hello you
现在我想做的是获取所有时间戳在3秒内的重复消息。所需输出为:
datetime user type msg
0 2012-11-11 15:41:08 u1 txt hello world
1 2012-11-11 15:41:11 u2 txt hello world
3 2012-11-22 18:08:35 u4 txt hello you
4 2012-11-22 18:08:37 u5 txt hello you
没有第三行,因为它的文本与第一和第二行相同,但其时间戳不在3秒范围内。
我尝试将datetime和msg列定义为
duplicate()
方法的参数,但由于时间戳不相同,它返回一个空数据框。mask = df.duplicated(subset=['datetime', 'msg'], keep=False)
print(df[mask])
Empty DataFrame
Columns: [datetime, user, type, msg, MD5]
Index: []
有没有一种方法可以为我的“datetime”参数定义范围?举个例子,像这样:
mask = df.duplicated(subset=['datetime_between_3_seconds', 'msg'], keep=False)
如往常一样,任何帮助都将不胜感激。
fillna(pd.Timedelta(seconds=0))
替换fillna(0)
。 - Molly Zhou