Pandas: Incrementally count occurrences in a column

You can use cumcount
to avoid a dummy column:

>>> df["Occ_Number"] = df.groupby("Name").cumcount()+1
>>> df
  Name  Occ_Number
0  abc           1
1  def           1
2  ghi           1
3  abc           2
4  abc           3
5  def           2
6  jkl           1
7  jkl           2

Leave a Comment