0

NBA dataset per year stats for players with different number of seasons played: ex:

Name         ,  Season,  Points,  Reb
Lebron James,   2003-04, 25ppg,   10red
Lebron James,   2004-05, 26ppg,   10reb
Lebron James,   2005-06, 27ppg,   9reb 
Michael Jordan, 1984-85, 29ppg,   9red
Michael Jordan, 1985-86, 30ppg,   10reb

How can I add a column that chronologically labels the years played for a player. So next to 2003-04 would be a 1 for lebron and 2004-05 would be a 2 etc. This would be different because all players have different rookie years and season played.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Hman
  • 1
  • [groupby cumcount](https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.GroupBy.cumcount.html) / [SO answer](https://stackoverflow.com/a/23435320/15497888)-> `df['counter'] = df.groupby('Name').cumcount() + 1` – Henry Ecker Oct 15 '21 at 19:27
  • Be warned however, with the shown data the column is actually `'Name '` not `'Name'`. I am unsure if that's a copy error or not. Additionally the sequential counter presupposes the seasons are in chronological order (as they are in the shown sample). – Henry Ecker Oct 15 '21 at 19:29

0 Answers0