Back to Practice
#0060
Keep Latest Event Per User
MediumPandas20 min20 XP
Problem
Given a pandas DataFrame with user_id, timestamp, and value columns, return a DataFrame containing the latest row for each user_id. Sort the result by user_id ascending and reset the index.
Why This Matters
Real event tables often contain many rows per entity. Latest-row extraction is used in churn labels, user profiles, fraud features, and dashboards.
Function Signature
def latest_event_per_user(events):
Examples
Example 1
Inputevents has two rows for user 1 and one row for user 2
Outputlatest user 1 row and user 2 row
Sorting by timestamp before deduplication makes keep="last" meaningful.
Constraints
- Do not mutate the input DataFrame.
- Sort final output by user_id.
- Reset the final index.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Latest row per user
InputDataFrame with repeated users
Expectedone latest row per user
The later timestamp wins for each user.
Hidden Test Categories
Already unique usersInput sorted in reverse timeThree events for the same user