Aviad Rozenhek
Aviad Rozenhek

Reputation: 2409

time between events by customer id using pandas

for the sample data below, I'd like to compute the mean time delta gap between events, grouped by customer id.

what's the best way to achieve this in pandas?

CUSTOMER_ID,    ORDER_AT
1,  2020-11-11 23:30:13
1,  2020-11-11 23:32:53
1,  2020-11-11 23:34:44
1,  2020-11-11 23:35:55
2,  2020-11-11 23:37:54
2,  2020-11-11 23:39:23
3,  2020-11-09 23:59:46
3,  2020-11-10 0:03:04
3,  2020-11-10 0:05:35
3,  2020-11-10 0:19:40
3,  2020-11-11 2:48:17
3,  2020-11-11 2:49:06
3,  2020-11-11 2:50:39
3,  2020-11-11 2:51:57
4,  2020-11-14 1:12:52
4,  2020-11-14 1:13:14
4,  2020-11-14 16:56:18

Upvotes: 0

Views: 34

Answers (1)

Stef
Stef

Reputation: 30609

df.groupby('CUSTOMER_ID').ORDER_AT.agg(lambda x: x.diff().mean())

Result:

CUSTOMER_ID
1             0 days 00:01:54
2             0 days 00:01:29
3   0 days 03:50:18.714285714
4             0 days 07:51:43

If you need the time delta as a number, e.g. as minutes, use x.diff().mean().seconds/60

Upvotes: 1

Related Questions