pandas 中唯一值的累积计数

2024-04-03

我想按周累计计算 pandas 框架中某一列的唯一值。例如,假设我有这样的数据:

df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,2,2],'module_id':['A','B','A','A','B','C']})
+---+---------+------+-----------+
|   | user_id | week | module_id |
+---+---------+------+-----------+
| 0 |       1 |    1 |         A |
| 1 |       1 |    1 |         B |
| 2 |       1 |    2 |         A |
| 3 |       2 |    1 |         A |
| 4 |       2 |    2 |         B |
| 5 |       2 |    2 |         C |
+---+---------+------+-----------+

我想要的是每周的唯一 module_id 数量的运行计数,即如下所示:

+---+---------+------+-------------------------+
|   | user_id | week | cumulative_module_count |
+---+---------+------+-------------------------+
| 0 |       1 |    1 |                       2 |
| 1 |       1 |    2 |                       2 |
| 2 |       2 |    1 |                       1 |
| 3 |       2 |    2 |                       3 |
+---+---------+------+-------------------------+

作为循环来执行此操作很简单,例如:

running_tally = {}
result = {}
for index, row in df.iterrows():
    if row['user_id'] not in running_tally:
        running_tally[row['user_id']] = set()
        result[row['user_id']] = {}
    running_tally[row['user_id']].add(row['module_id'])
    result[row['user_id']][row['week']] = len(running_tally[row['user_id']])
print(result)
{1: {1: 2, 2: 2}, 2: {1: 1, 2: 3}}

但我的真实数据帧非常巨大,所以我想要一个矢量化算法而不是循环。

有一个听起来类似的问题here https://stackoverflow.com/q/35759120/575530,但看看已接受的答案(here https://stackoverflow.com/a/35759315/575530)原始发帖者不希望像我一样在日期之间累积唯一性。

我将如何在 pandas 中进行矢量化?


想法就是创造lists 每组按两列,然后使用np.cumsum对于累积列表,最后将值转换为集合并获取长度:

df1 = (df.groupby(['user_id','week'])['module_id']
         .apply(list)
         .groupby(level=0)
         .apply(np.cumsum)
         .apply(lambda x: len(set(x)))
         .reset_index(name='cumulative_module_count'))

print (df1)
   user_id  week  cumulative_module_count
0        1     1                        2
1        1     2                        2
2        2     1                        1
3        2     2                        3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pandas 中唯一值的累积计数 的相关文章

随机推荐