learner
learner

Reputation: 194

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?

Upvotes: 0

Views: 46

Answers (1)

ArchAngelPwn
ArchAngelPwn

Reputation: 3046

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)

Upvotes: 2

Related Questions