I have a pandas dataframe like this:
CustomerId Timestamp
0. a 01-09-2018 00:08:00
1. a 01-09-2018 00:09:00
2. b 01-09-2018 00:11:00
3. b 01-09-2018 00:15:00
I need to calculate the difference in minutes between consecutive timestamps for each customer so that in the end I have something that looks a little like this:
CustomerId Timestamp Difference
0. a 01-09-2018 00:08:00 -
1. a 01-09-2018 00:09:00 1
2. b 01-09-2018 00:11:00 -
3. b 01-09-2018 00:15:00 4
I have been trying some loops but nothing seems to be working out. I would really appreciate it if someone could help me out :)
Using groupby
with diff
df.groupby('CustomerId').Timestamp.diff().dt.total_seconds()/60
Out[10]:
0.0 NaN
1.0 1.0
2.0 NaN
3.0 4.0
Name: Timestamp, dtype: float64
df['Different']=df.groupby('CustomerId').Timestamp.diff().dt.total_seconds()/60
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.