-1

In my dataframe are several patient IDs and some patients appear several times in the dataframe, which is correct. Now the patients need to get a number of the times they appear in the dataframe.

Example:

1

Wanted:

2

How can I get the occurrence number of the IDs?

Thank you for your help!!

vitaliis
  • 4,082
  • 5
  • 18
  • 40

1 Answers1

0

Does this work:

df['Occurence'] = df.groupby('ID').cumcount() + 1
df
    ID  Data    Occurence
0   123 asd     1
1   123 qwe     2
2   451 cvb     1
3   784 tds     1
4   784 äkh     2
5   784 ijh     3
Karthik S
  • 11,348
  • 2
  • 11
  • 25