Skip to content
Advertisement

Merging segments from the same trips into a single trip for analysis

In the MWE below, I show my attempt to line-plot trips (from my df aggregated per month):

JavaScript

I realised in my df, some trips contains jump (maybe due to data log), so they should be merged into single trip before aggregation. In the given df example above (before grouping).

JavaScript

User 154 does undertake 2-trips, not 3. First started at 10:10:00 on bus then changes travel mode to at 10:36:00 to walk. Also user 62 does only a trip, started at 04:21:40 - 05:33:46 on a bus, then stopped, waited for about 6-minutes then continued to at about 05:40:31.

Question

I want to merge such trips into a single trip so that if the time interval between the end of a user’s trip to beginning of the next is less than 30 minutes, that should be considered a single trip. Then I can plot,

In the above MWE, the aggregated trips are “over-estimated”, the expected final df should be:

JavaScript

Advertisement

Answer

You can use a custom function:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement