【问题标题】:Pandas: Find repeat edits (consecutive) - all consecutive edits have happened in small time windowPandas:查找重复编辑(连续) - 所有连续编辑都发生在小时间窗口内
【发布时间】:2021-05-09 16:16:48
【问题描述】:

使用用户上传文档和编辑文档的数据集。目标是识别相同的重复/连续编辑。

要符合连续重复编辑的条件,编辑必须由同一用户、同一文档在小时间窗口内完成,编辑类型应为editwordsAdded 应相同。

editTime               userId   docId     editType  wordsAdded
2000-01-01-13:23:12        101     p101       edit        05
2000-01-01-13:24:00        101     p101       edit        05
2000-01-01-13:24:10        101     p101       edit        05
2000-01-01-13:24:11        101     p101       edit        05
2000-01-01-13:25:00        101     p101       edit        05
2000-01-01-13:25:13        101     p101       edit        10
2000-01-01-13:24:14        101     p101       edit        1
2000-01-01-15:28:12        101     d101       upload      00
2000-01-01-15:30:00        101     d101       edit         2
2000-01-01-15:30:01        101     d101       edit         2
2000-01-01-15:30:04        101     p101       edit        12
2000-01-01-15:30:10        101     p101       edit        12
2000-01-01-15:30:11        101     p101       edit        12
2000-01-03-11:45:01        101     p102       edit        44
2000-01-04-11:45:03        101     c101       edit        44
2000-01-03-09:32:04        300     c201       edit        05
2000-01-03-13:33:05        300     c301       edit        04
2000-01-04-15:12:06        300     c401       edit        19
2000-01-04-15:12:10        300     c401       edit        19
2000-01-05-16:32:08        300     c401       edit        32

在上述数据集中,有效的连续编辑是:

2000-01-01-13:23:12        101     p101       edit        05
2000-01-01-13:24:00        101     p101       edit        05
2000-01-01-13:24:10        101     p101       edit        05
2000-01-01-13:24:11        101     p101       edit        05
2000-01-01-13:25:00        101     p101       edit        05

2000-01-01-15:30:00        101     d101       edit         2
2000-01-01-15:30:01        101     d101       edit         2
2000-01-01-15:30:04        101     p101       edit        12
2000-01-01-15:30:10        101     p101       edit        12

2000-01-04-15:12:06        300     c401       edit        19
2000-01-04-15:12:10        300     c401       edit        19

我的做法是:

将时间转换为纪元:

df['editTime'] = df['editTime'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d-%H:%M:%S").timestamp())

multi_edits = df[df.duplicated(
    ['userId', 
     'docId', 
     'wordsAdded', 
     'editType'], 
    keep=False)].where(
    df.editType == 'edit').sort_values(
    ['userId','docId', 'editType', 'editTime']
)

为了获取时间跨度添加了一个方法:

def time_span(df: pd.DataFrame):
    df['time_diff'] = df['editTime'].diff(1)
    return df

然后按multi_edits 分组,得到连续编辑总数和字数

multi_edit_sum_count = multi_edits.groupby(
    ['userId', 
     'docId', 
     'wordsAdded', 
     'editType']).apply(time_span)

multi_edit_30s = multi_edit_sum_count[
    multi_edit_sum_count.time_diff < 30].agg(
    {'wordsAdded':'sum', 'userId':'count'})

我认为这不是正确的方法。在 pandas 中也可能有更好的方法来做到这一点。

【问题讨论】:

  • 为了澄清,您只是在寻找在 30 秒内发生的重复编辑总数以及唯一用户数?

标签: python python-3.x pandas dataframe


【解决方案1】:

我们可以编写一些逻辑来检查时间差是否最大为 60 秒(您可以根据需要对其进行编辑)以及组是否超过 1 行:

def check_time_difference(s):
    fill_value = pd.Timedelta(0)
    m1 = s.diff().fillna(fill_value).dt.seconds.le(60).all()
    m2 = s.shape[0] > 1
    
    return m1 & m2


grp_cols = ["userId", "docId", "editType", "wordsAdded"]
df[df.groupby(grp_cols)["editTime"].transform(check_time_difference)]

输出


              editTime  userId docId editType  wordsAdded
0  2000-01-01 13:23:12     101  p101     edit           5
1  2000-01-01 13:24:00     101  p101     edit           5
2  2000-01-01 13:24:10     101  p101     edit           5
3  2000-01-01 13:24:11     101  p101     edit           5
4  2000-01-01 13:25:00     101  p101     edit           5
8  2000-01-01 15:30:00     101  d101     edit           2
9  2000-01-01 15:30:01     101  d101     edit           2
10 2000-01-01 15:30:04     101  p101     edit          12
11 2000-01-01 15:30:10     101  p101     edit          12
12 2000-01-01 15:30:11     101  p101     edit          12
17 2000-01-04 15:12:06     300  c401     edit          19
18 2000-01-04 15:12:10     300  c401     edit          19

【讨论】:

  • 所以s 是正确的一行,因为这是在groupby 级别调用的?或 s 是 s: pd.DataFrame
  • 获得TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多