0

I am attempting to make a column within my data frame that lists out the day number based on the count of each occurrence of another column. I want the counter to start again at 0 for every new group. Any help would be much appreciated!

I want it to look like this:

| Group   | Date      | Day |
| ------- | --------- | --- |
|    a    | 1/1/2022  |  0  |
|    a    | 1/2/2022  |  1  |
|    a    | 1/3/2022  |  2  |
|    b    | 1/1/2022  |  0  |
|    b    | 1/2/2022  |  1  |
|    b    | 1/3/2022  |  2  |
|    c    | 1/1/2022  |  0  |
|    c    | 1/2/2022  |  1  |
|    c    | 1/3/2022  |  2  |

1 Answers1

0

Given the Group and Date column, this might be, what you are looking for:

df['Day'] = df.groupby('Group').cumcount()
vogelstein
  • 394
  • 1
  • 10