Skip to content
Advertisement

Enumerate rows in each group starting from one

I have a dataframe (which is sorted on date, date column is not included in the example for simplicity) that looks like this:

JavaScript

I want to create a new column that counts the occurrence of each value in the letters column, increasing 1 by 1 as the value occurs in the letters column. The data frame I want to reach is like this:

JavaScript

Any help would be amazing, thanks in advance!

Advertisement

Answer

You can groupby and use the method cumcount:

JavaScript

Result:

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