Deduplicate a pandas DataFrame on Key Columns, Keeping the Last Occurrence
Quick Overview
Write a pandas function that removes rows duplicated on a set of key columns, keeps the last row of each group, preserves row and column order and resets the index. Tests pandas deduplication options, index handling, missing key values and how to define "last" by a timestamp.
Deduplicate a pandas DataFrame on Key Columns, Keeping the Last Occurrence
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `dedupe_keep_last(df, subset_cols)`, which removes rows duplicated on the columns in `subset_cols` (other columns are ignored for comparison), keeping the last occurrence of each group (`keep='last'`). Return a new DataFrame. You may assume every name in `subset_cols` exists in `df`.
Requirements:
- Preserve the original column order and the relative order of surviving rows.
- Reset the index with `reset_index(drop=True)`.
- Do not mutate the input `df`.
Example:
```python
df = pd.DataFrame({
'user_id': [1, 2, 1, 3, 2],
'score': [10, 20, 30, 40, 50],
})
dedupe_keep_last(df, ['user_id'])
# user_id score
# 0 1 30 # last row for user_id 1
# 1 3 40
# 2 2 50 # last row for user_id 2
```
```hint Defaults to check
Look at the default value of the argument that decides which copy of a group survives, and at what happens to the index labels once rows are removed.
```
### What a Strong Answer Covers
- Restricts the duplicate check to the columns in `subset_cols` and keeps the last row of each group.
- Keeps the surviving rows in their original relative order and the columns in their original order.
- Renumbers the index from 0 without adding the old labels as a column.
- Returns a new DataFrame and leaves the input unchanged.
- States the cost of the pass and how edge cases such as missing key values behave.
### Follow-up Questions
- How would you keep the last row per group by a `timestamp` column instead of by row position?
- Why prefer returning a copy over mutating the DataFrame in place?
- Some rows have a missing value in a key column. How does your function group them, and how would you change that if those rows should be kept or dropped?
- How would you write the same deduplication in SQL, where a table has no row order?
Overview: Write a pandas function that removes rows duplicated on a set of key columns, keeps the last row of each group, preserves row and column order and resets the index. Tests pandas deduplication options, index handling, missing key values and how to define "last" by a timestamp.
Implement dedupe_keep_last(df, subset_cols), which removes rows duplicated on the columns in subset_cols (other columns are ignored for comparison), keeping the last occurrence of each group (keep='last'). Return a new DataFrame. You may assume every name in subset_cols exists in df.
Requirements:
Preserve the original column order and the relative order of surviving rows.
Restricts the duplicate check to the columns in
subset_cols
and keeps the last row of each group.
Keeps the surviving rows in their original relative order and the columns in their original order.
Renumbers the index from 0 without adding the old labels as a column.
Returns a new DataFrame and leaves the input unchanged.
States the cost of the pass and how edge cases such as missing key values behave.
Follow-up Questions Guidance
How would you keep the last row per group by a
timestamp
column instead of by row position?
Why prefer returning a copy over mutating the DataFrame in place?
Some rows have a missing value in a key column. How does your function group them, and how would you change that if those rows should be kept or dropped?
How would you write the same deduplication in SQL, where a table has no row order?