I have a dataframe (which is sorted on date, date column is not included in the example for simplicity) that looks like this:
JavaScript
x
10
10
1
df = pd.DataFrame(['A', 'B' , 'B', 'A', 'C', 'B'], columns=['letters'])
2
3
letters
4
0 A
5
1 B
6
2 B
7
3 A
8
4 C
9
5 B
10
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
1
8
1
letters occurance_counter
2
0 A 1
3
1 B 1
4
2 B 2
5
3 A 2
6
4 C 1
7
5 B 3
8
Any help would be amazing, thanks in advance!
Advertisement
Answer
You can groupby
and use the method cumcount
:
JavaScript
1
2
1
df['occurance_counter'] = df.groupby('letters')['letters'].cumcount().add(1)
2
Result:
JavaScript
1
8
1
letters occurance_counter
2
0 A 1
3
1 B 1
4
2 B 2
5
3 A 2
6
4 C 1
7
5 B 3
8