Keep each user's latest row in a pandas DataFrame

Quick Overview

Write a pandas function that keeps only each user's most recent event row and returns the result sorted by user with a clean 0..n-1 index. It tests sorting, deduplication, tie handling and missing timestamps in everyday DataFrame cleanup.

Keep each user's latest row in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You are given a pandas DataFrame `df` with columns `['user_id', 'event_ts', 'status']`, where `event_ts` already has dtype `datetime64[ns]`. Implement `keep_latest_per_user(df)` so that it returns one row per user: the row with that user's most recent `event_ts`. The function must: - Sort ascending with `sort_values('event_ts')`, then apply `drop_duplicates('user_id', keep='last')`. - Sort the result ascending by `user_id`, then call `reset_index(drop=True)` so the index runs `0..n-1`. - Return all three columns, in the order `['user_id', 'event_ts', 'status']`. Example: ```text Input: user_id event_ts status 1 2024-01-01 09:00 open 1 2024-01-03 12:00 closed 2 2024-01-02 08:00 open Output: user_id event_ts status 1 2024-01-03 12:00 closed 2 2024-01-02 08:00 open ``` User 1 has two rows and the 2024-01-03 row is the later one, so it is kept with status `closed`. User 2 has a single row, which is kept unchanged. ```hint Check the sort before trusting it Look at what `sort_values` guarantees for rows that share the same `event_ts`, and where it places missing timestamps, before you rely on which row ends up last. ``` ### Constraints & Assumptions - `df` has exactly the columns `user_id`, `event_ts` and `status`. - `event_ts` is already `datetime64[ns]`, so no parsing is needed. - A user can have any number of rows, and `user_id` values can be sorted. ### What a Strong Answer Covers - Why sorting by time before `drop_duplicates(keep='last')` leaves each user's latest row. - The required output shape: ordered by `user_id`, a clean `0..n-1` index, and the three columns in order. - What happens when a user has two rows with the same `event_ts`, and how to make the winner deterministic. - Edge cases such as missing timestamps and an empty input. - Time and space complexity. ### Follow-up Questions - How would you break ties when a user has two rows with identical `event_ts`? - How does this compare to `df.loc[df.groupby('user_id')['event_ts'].idxmax()]`? - What changes if some `event_ts` values are `NaT`?

Overview: Write a pandas function that keeps only each user's most recent event row and returns the result sorted by user with a clean 0..n-1 index. It tests sorting, deduplication, tie handling and missing timestamps in everyday DataFrame cleanup.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

You are given a pandas DataFrame df with columns ['user_id', 'event_ts', 'status'], where event_ts already has dtype datetime64[ns]. Implement keep_latest_per_user(df) so that it returns one row per user: the row with that user's most recent event_ts.

The function must:

  • Sort ascending with sort_values('event_ts') , then apply drop_duplicates('user_id', keep='last') .
  • Sort the result ascending by user_id , then call reset_index(drop=True) so the index runs 0..n-1 .
  • Return all three columns, in the order ['user_id', 'event_ts', 'status'] .

Example:

Input:
user_id  event_ts             status
1        2024-01-01 09:00     open
1        2024-01-03 12:00     closed
2        2024-01-02 08:00     open

Output:
user_id  event_ts             status
1        2024-01-03 12:00     closed
2        2024-01-02 08:00     open

User 1 has two rows and the 2024-01-03 row is the later one, so it is kept with status closed. User 2 has a single row, which is kept unchanged.

Constraints & Assumptions

  • df has exactly the columns user_id , event_ts and status .
  • event_ts is already datetime64[ns] , so no parsing is needed.
  • A user can have any number of rows, and user_id values can be sorted.

What a Strong Answer Covers Guidance

  • Why sorting by time before drop_duplicates(keep='last') leaves each user's latest row.
  • The required output shape: ordered by user_id , a clean 0..n-1 index, and the three columns in order.
  • What happens when a user has two rows with the same event_ts , and how to make the winner deterministic.
  • Edge cases such as missing timestamps and an empty input.
  • Time and space complexity.

Follow-up Questions Guidance

  • How would you break ties when a user has two rows with identical event_ts ?
  • How does this compare to df.loc[df.groupby('user_id')['event_ts'].idxmax()] ?
  • What changes if some event_ts values are NaT ?