'Python: Increment number based on name and unique year

I am wondering if there is an efficient way in python to increment the occurrences of the person's name based on the year they appear?

For example, consider the dataframe:

|    | Name   |   year |
|---:|:-------|-------:|
|  0 | Tom    |   2000 |
|  1 | Joseph |   2001 |
|  2 | Brian  |   1990 |
|  3 | Tom    |   2005 |
|  4 | Tom    |   2006 |
|  5 | Joseph |   2008 |
|  6 | Brian  |   1990 |

I would like the result to show the following:

Tom = 0,1,2
Joseph = 0, 1 
Brian = 0

Tom has appeared in three unique years so the result would be 0, 1, 2. Likewise, although Brian appears twice but has the same year, hence result should be 0.



Solution 1:[1]

Let us try groupby + rank

df['counter'] = df.groupby('Name')['year'].rank(method='dense') - 1

print(df)

     Name  year  counter
0     Tom  2000      0.0
1  Joseph  2001      0.0
2   Brian  1990      0.0
3     Tom  2005      1.0
4     Tom  2006      2.0
5  Joseph  2008      1.0
6   Brian  1990      0.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Sharma