4

I have created a DataFrame, and now need to count each duplicate row (by for example df['Gender']. Suppose Gender 'Male' occurs twice and Female three times, I need this column to be made:

Gender   Occurrence
Male     1
Male     2
Female   1
Female   2
Female   3

Is there a way to do this with Pandas?

cottontail
  • 10,268
  • 18
  • 50
  • 51
J. Williams
  • 135
  • 3
  • 10
  • Does this answer your question? [Add a sequential counter column on groups to a pandas dataframe](https://stackoverflow.com/questions/23435270/add-a-sequential-counter-column-on-groups-to-a-pandas-dataframe) – cottontail Sep 18 '22 at 02:34

1 Answers1

8

Use the cumcount method after grouping by Gender:

df = pd.DataFrame({'Gender':['Male','Male','Female','Female','Female']})   
df['Occurrence'] = df.groupby('Gender').cumcount() + 1
print(df)

   Gender  Occurrence
0    Male           1
1    Male           2
2  Female           1
3  Female           2
4  Female           3

Counts start with 0 so I added a + 1 there.

gereleth
  • 2,452
  • 12
  • 21