Issue
Simple question but I am struggling to find the answer. I am after a simple count of days from the start date. Thanks in advance.
df = pd.DataFrame({'year': [2015, 2015, 2015],
'month': [2, 2, 2],
'day': [4, 5, 10]})
pd.to_datetime(df)
output
0 2015-02-04
1 2015-02-05
2 2015-02-10
What I want
Date Days_from_start
0 2015-02-04 0
1 2015-02-05 1
2 2015-02-10 6
Solution
Create a timedelta by subtracting the reference, then access the days with dt.days
:
out = pd.to_datetime(df).to_frame('Date')
out['Days_from_start'] = out['Date'].sub(out['Date'].iloc[0]).dt.days
# or if you want from the oldest day
out['Days_from_start'] = out['Date'].sub(out['Date'].min()).dt.days
Output:
Date Days_from_start
0 2015-02-04 0
1 2015-02-05 1
2 2015-02-10 6
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.