【发布时间】:2021-06-25 02:46:22
【问题描述】:
我有一个数据框,我想在其中识别(并最终删除)滑动时间窗口内的重复行。
dict={
'type': ['apple','apple','apple','berry','grape','apple'],
'attr': ['red','green','red','blue','green','red'],
'timestamp': [ '2021-03-01 12:00:00',
'2021-03-01 12:00:30',
'2021-03-01 12:01:13',
'2021-03-01 12:01:30',
'2021-03-01 12:10:00',
'2021-03-01 12:11:00',
]
}
df = pd.DataFrame(dict)
df['is_dup'] = False
print(df)
type attr timestamp is_dup
0 apple red 2021-03-01 12:00:00 False
1 apple green 2021-03-01 12:00:30 False
2 apple red 2021-03-01 12:01:13 False
3 berry blue 2021-03-01 12:01:30 False
4 grape green 2021-03-01 12:10:00 False
5 apple red 2021-03-01 12:11:00 False
在示例中,我的目标是在 'type' 和 'attr' 等于 2 分钟内发生的另一行时将一行标记为重复。所以我想标记索引 2 is_dup=True 因为它匹配索引 0 并且在 2 分钟的时间范围内,但不是第 5 行,因为它的时间戳不在窗口内。
所以生成的数据框看起来像:
type attr timestamp is_dup
0 apple red 2021-03-01 12:00:00 False
1 apple green 2021-03-01 12:00:30 False
2 apple red 2021-03-01 12:01:13 True
3 berry blue 2021-03-01 12:01:30 False
4 grape green 2021-03-01 12:10:00 False
5 apple red 2021-03-01 12:11:00 False
提前致谢。
【问题讨论】:
-
索引0不应该也是
is_dup=True吗? -
我不希望原件被视为复制品。稍后我将返回并删除所有 is_dup = True 的行,在这种情况下我不希望删除原始数据。