0

I'm trying to create a new column that counts the number of times the date changed based on a unique identifier from another column.

Example:

Date -     ID Code -   Number of Changes
01.03.21   B017               0
02.03.21   B017               1
01.03.21   A018               0
03.03.21   A019               0
04.03.21   A019               1
06.03.21   A019               2
07.03.21   A021               0

So if the date has changed one time for the ID Code B017 then it'll record 1, and if it has changed twice it will record 2 etc. and do the same for every individual ID in the ID Code column.

While I know how to create a count of the number of changes in the date column overall, I haven't figured out a simple way to do these counts based on the ID Code.

Any help would be appreciated.

Ak17
  • 13
  • 4

1 Answers1

0

Assuming there are no duplicates of the same data for the same id, then you can use groupby together with cumcount as follows:

df['Number of Changes'] = df.groupby('ID Code').cumcount()

Result:

        Date    ID Code Number of Changes
0   01.03.21       B017                 0
1   02.03.21       B017                 1
2   01.03.21       A018                 0
3   03.03.21       A019                 0
4   04.03.21       A019                 1
5   06.03.21       A019                 2
6   07.03.21       A021                 0
Shaido
  • 27,497
  • 23
  • 70
  • 73