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

Creating day count using pandas

I have following dataframe,

ID d_of_stay
1 2021-03-01
1 2021-03-02
1 2021-03-03
2 2021-03-05
2 2021-03-06

I have to create a column like below,

ID d_of_stay day
1 2021-03-01 Day 0
1 2021-03-02 Day 1
1 2021-03-03 Day 2
2 2021-03-05 Day 0
2 2021-03-06 Day 1

How to do that using pandas/python?

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 :

This will give you the results you expect IIUC

df['day'] = df.groupby('ID')['d_of_stay'].cumcount()
df['day'] = 'Day ' + df['day'].astype(str)
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