I have a following dataframe:
col
0 A
1 B
2 A
3 A
4 A
5 A
6 A
7 B
8 B
9 A
I want to enumerate duplicated values of the column col
in an efficient way starting with 1 so the dataset should be like this:
col counts
0 A 1
1 B 1
2 A 2
3 A 3
4 A 4
5 A 5
6 A 6
7 B 2
8 B 3
9 A 7
I tried this code that works fine but too slow.
counter = defaultdict(int)
counts = []
for _, row in df.iterrows():
counter[row['col']] += 1
counts.append(counter[row['col']])
df['counts'] = counts
Is there a better and faster way to do the same?