Reputation: 293
Trying to find the date difference grouping by ID and using the first date by ID as the starting point.
Input
ID Date
1 1/1/2020
1 1/1/2020
1 1/5/2020
2 1/1/2020
2 1/3/2020
2 1/10/2020
Output
ID Date DateDiff
1 1/1/2020 0
1 1/1/2020 0
1 1/5/2020 4
2 1/1/2020 0
2 1/3/2020 2
2 1/10/2020 9
Upvotes: 1
Views: 193
Reputation: 29635
you can use groupby.transform
and first
.
df['DateDiff'] = pd.to_datetime(df['Date']) #to work with datetime object
df['DateDiff'] = (df['DateDiff']-
df['DateDiff'].groupby(df['ID']).transform('first')).dt.days
print(df)
ID Date DateDiff
0 1 1/1/2020 0
1 1 1/1/2020 0
2 1 1/5/2020 4
3 2 1/1/2020 0
4 2 1/3/2020 2
5 2 1/10/2020 9
Upvotes: 3