Skip to content
Advertisement

How to to cumcount in group for specific value?

I have a dataframe:

JavaScript

when I do df.groupby("id").cumcount() it returns:

JavaScript

I want to count only those ones that equal w in column value and it must be in dataframe form:

JavaScript

How to do that with cumcount function?

Advertisement

Answer

Use:

JavaScript

Output

JavaScript

As an alternative:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement