I'm trying to solve a problem with pandas that consists of increment the column "duration" but in those rows that the pair "id" and "user_number" on consecutive events. I've tried several ways using groupby or boolean columns with cumsum but it will always accumulate values of events (or rows) that aren't consecutives.
I have a sample of the data:
id user_num user_key start_time end_time type duration
1185ba 413 221528661 2020-12-13 15:15:00.000 2020-12-13 15:34:09.000 L 1149000.0
1185ba 403 22251339 2020-12-13 15:34:09.000 2020-12-13 15:49:00.000 L 891000.0
1185ba 403 22421785 2020-12-13 15:49:00.000 2020-12-13 16:10:00.000 L 1260000.0
6666uj 403 22621565 2020-12-13 16:10:00.000 2020-12-13 16:37:00.000 L 1620000.0
1185ba 490 221493747 2020-12-13 16:37:05.000 2020-12-13 16:49:00.000 L 715000.0
1185ba 490 221063609 2020-12-13 16:49:00.000 2020-12-13 16:50:45.000 L 105000.0
1185ba 413 221528653 2020-12-13 16:50:45.000 2020-12-13 16:51:06.463 L 21463.0 0
1185ba 11 12470010 2020-12-13 16:51:06.463 2020-12-13 16:55:00.000 NL 233537.0
1185ba 413 221528654 2020-12-13 16:55:00.000 2020-12-13 17:00:00.000 L 300000.0
1185ba 413 562281418 2020-12-13 17:00:00.000 2020-12-13 17:25:00.000 L 1500000.0
1185ba 413 221528638 2020-12-13 17:25:00.000 2020-12-13 17:31:30.013 L 390013.0
And this example is what I want to get:
id user_num user_key start_time end_time type duration (sum)
1185ba 413 221528661 2020-12-13 15:15:00.000 2020-12-13 15:34:09.000 L 1149000.0
1185ba 403 22251339 2020-12-13 15:34:09.000 2020-12-13 15:49:00.000 L 891000.0
1185ba 403 22421785 2020-12-13 15:49:00.000 2020-12-13 16:10:00.000 L 2151000.0
6666uj 403 22621565 2020-12-13 16:10:00.000 2020-12-13 16:37:00.000 L 1620000.0
1185ba 490 221493747 2020-12-13 16:37:05.000 2020-12-13 16:49:00.000 L 715000.0
1185ba 490 221063609 2020-12-13 16:49:00.000 2020-12-13 16:50:45.000 L 820000.0
1185ba 413 221528653 2020-12-13 16:50:45.000 2020-12-13 16:51:06.463 L 21463.0
1185ba 11 2470010 2020-12-13 16:51:06.463 2020-12-13 16:55:00.000 NL 233537.0
1185ba 413 221528654 2020-12-13 16:55:00.000 2020-12-13 17:00:00.000 L 300000.0
1185ba 413 562281418 2020-12-13 17:00:00.000 2020-12-13 17:25:00.000 L 1800000.0
1185ba 413 221528638 2020-12-13 17:25:00.000 2020-12-13 17:31:30.013 L 2190013.0
How could I get a table like this? I'm having a hard time in search things like this. we have a user_num with 413 and id and id 1185ba that starts at 15:15 but there are events in the middle until reaches the line 1185ba 413 221528653 2020-12-13 16:50:45.000 2020-12-13 16:51:06.463 L 21463.0 and the sum or cumsum is restarted.
But the key points it's id with user_key and the duration which must be summed if the pair of key points are consecutive, when this doesn't happen anymore the count for the pair is reset.