我有数据框
id webpage
1 google
2 bing
3 google
4 google
5 yahoo
6 yahoo
7 google
8 google
想数群
喜欢
id webpage count
1 google 1
2 bing 2
3 google 3
4 google 3
5 yahoo 4
6 yahoo 4
7 google 5
8 google 5
我曾尝试在使用 groupby 时使用 cumcount
或 ngroup
对所有事件进行分组。
最佳答案
我相信您需要 cumsum()
处理状态转换。每次 webpage
与上一行不同时,您都会增加 count
。
df["count"] = (df.webpage != df.webpage.shift()).cumsum()
关于python - 计算组出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73842710/