-1

I'm working with sports data where a competitor has 3 rounds. The data doesn't have a column saying which round it is - but the data is in sequence (i.e. first round is at the top, followed by 2nd round). How do I create a column that adds the round number based on the sequence the data is in? I've tried creating a counter - but can't get it to reset based on the competitor and competition

Thanks

i.e.

Competitor  Round Score 
X            1     4
X            2     4.5 
X            3     4.2
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42

1 Answers1

0

Use -

df['Round'] = df.groupby('Competitor').cumcount() + 1

Since you have not provided data example I won't be able to show the result. Here is some sample input data -

  Competitor  Score
0          X    4.0
1          X    4.5
2          X    4.2
3          Y    4.0
4          Y    4.5

Output

  Competitor  Score  Round
0          X    4.0      1
1          X    4.5      2
2          X    4.2      3
3          Y    4.0      1
4          Y    4.5      2
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42