0

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?

1
  • Explanation of the result? Commented Jun 29, 2022 at 17:05

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Oh no I misread thought he started at 1 i'll update thank you
It should be without +1, so it starts from 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.