Issue
I have dataframe
id webpage
1 google
2 bing
3 google
4 google
5 yahoo
6 yahoo
7 google
8 google
Would like to count the groups
like
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
I have tried using the cumcount
or ngroup
when using groupby it is grouping all occurrence.
Solution
I believe you need to cumsum()
over the state transitions. Every time webpage
differs from the previous row you increase your count
.
df["count"] = (df.webpage != df.webpage.shift()).cumsum()
Answered By - filippo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.