Sessionise a Clickstream

medium
Unit 10 · Pandas III0/2 to milestone

Web analytics groups each user's events into sessions: a new session starts when the gap since that user's previous event is more than timeout seconds (Google Analytics uses 30 minutes).

events has columns user and ts (integer seconds), in any order. Implement:

  • sessionize(events, timeout=1800): return a copy with a session_id column formatted f"{user}-{k}", where k counts that user's sessions from 1 in time order. Keep the original row order and index.
  • session_summary(events, timeout=1800): one row per session with columns session_id, user, start, end, n_events and duration (end − start), sorted by user then start, with a fresh index
#pandas#product-analytics#time-series2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Sessionise a Clickstream · TechGradify