Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making a Count of Days Column Grouped by Another Column (pandas)

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  |

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

df['Day'] = df.groupby('Group').cumcount()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading