Mark earlier duplicate rows in a pandas DataFrame with keep='last'
Quick Overview
Write a pandas function that returns a boolean mask marking every DataFrame row that has an identical row later in the frame, so only the last copy of each group stays unflagged. It tests keep='last' semantics, index alignment, and how the mask compares with keep='first' and keep=False.
Mark earlier duplicate rows in a pandas DataFrame with keep='last'
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `mark_last_occurrence(df)` that returns a mask of fully duplicated rows, keeping the last occurrence of each group. A row is `True` when an identical row appears later in the frame. The last copy of each group and unique rows are `False`.
Requirements:
- Return a boolean `Series` (dtype `bool`) aligned to the input's index.
- Rows are duplicates only if every column value matches.
- Preserve the original index (no reset or sort). The result's `name` must be `None`.
- Do not drop rows: return only the mask.
Example:
```python
df = pd.DataFrame({'a': [1, 1, 2], 'b': ['x', 'x', 'y']})
mark_last_occurrence(df)
# 0 True # identical row appears later at index 1
# 1 False # last occurrence of (1, 'x')
# 2 False # unique
# dtype: bool
```
```hint Which copy survives
Write out a group of three identical rows and decide which one stays `False`. Then check which option of pandas' row-level duplicate check picks that copy.
```
### What a Strong Answer Covers
- Which copy of each duplicate group stays `False` and how the chosen call produces that
- How the mask keeps the input index, the `bool` dtype and a `None` name without dropping or reordering rows
- How the result compares with `keep='first'` and `keep=False` on the same frame, including how many rows each flags
- How missing values, index labels and unhashable cells affect the comparison
### Follow-up Questions
- How does the result differ between `keep='last'`, `keep='first'` and `keep=False`?
- How would you dedupe on a subset of columns instead of all of them?
- When is keeping the last occurrence the right choice in a real cleaning job, and what has to be true about row order?
- How would you build the same mask without calling `duplicated()`?
Overview: Write a pandas function that returns a boolean mask marking every DataFrame row that has an identical row later in the frame, so only the last copy of each group stays unflagged. It tests keep='last' semantics, index alignment, and how the mask compares with keep='first' and keep=False.
Implement mark_last_occurrence(df) that returns a mask of fully duplicated rows, keeping the last occurrence of each group. A row is True when an identical row appears later in the frame. The last copy of each group and unique rows are False.
Requirements:
Return a boolean
Series
(dtype
bool
) aligned to the input's index.
Rows are duplicates only if every column value matches.
Preserve the original index (no reset or sort). The result's
name
must be
None
.
Do not drop rows: return only the mask.
Example:
df = pd.DataFrame({'a': [1, 1, 2], 'b': ['x', 'x', 'y']})
mark_last_occurrence(df)
# 0 True # identical row appears later at index 1
# 1 False # last occurrence of (1, 'x')
# 2 False # unique
# dtype: bool
What a Strong Answer Covers Guidance
Which copy of each duplicate group stays
False
and how the chosen call produces that
How the mask keeps the input index, the
bool
dtype and a
None
name without dropping or reordering rows
How the result compares with
keep='first'
and
keep=False
on the same frame, including how many rows each flags
How missing values, index labels and unhashable cells affect the comparison
Follow-up Questions Guidance
How does the result differ between
keep='last'
,
keep='first'
and
keep=False
?
How would you dedupe on a subset of columns instead of all of them?
When is keeping the last occurrence the right choice in a real cleaning job, and what has to be true about row order?
How would you build the same mask without calling
duplicated()
?