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:

df = pd.DataFrame(['A', 'B' , 'B', 'A', 'C', 'B'], columns=['letters'])

  letters
0       A
1       B
2       B
3       A
4       C
5       B

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:

  letters  occurance_counter
0       A                  1
1       B                  1
2       B                  2
3       A                  2
4       C                  1
5       B                  3

Any help would be amazing, thanks in advance!

Advertisement

Answer

You can groupby and use the method cumcount:

df['occurance_counter'] = df.groupby('letters')['letters'].cumcount().add(1)

Result:

  letters  occurance_counter
0       A                  1
1       B                  1
2       B                  2
3       A                  2
4       C                  1
5       B                  3
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement