Remove Exact Duplicate Rows from a pandas DataFrame
Quick Overview
Write a pandas function that drops rows identical across every column, keeps the first copy, resets the index and leaves the input DataFrame untouched. Tests how well you know pandas deduplication, index handling and edge cases such as missing values and unhashable cells.
Remove Exact Duplicate Rows from a pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def remove_exact_duplicates(df: pd.DataFrame) -> pd.DataFrame` that drops rows identical across all columns, keeping the first occurrence. Return a new DataFrame.
Requirements:
- Compare rows on all columns (no subset) and keep the first occurrence.
- Reset the index to a fresh `0..n-1` RangeIndex (`ignore_index=True`).
- Preserve the same columns and dtypes.
- Do not mutate the input `df`.
Example:
```python
df = pd.DataFrame({"a": [1, 1, 2], "b": ["x", "x", "y"]})
remove_exact_duplicates(df)
# a b
# 0 1 x
# 1 2 y
```
```hint Where to look
pandas already has a method that compares whole rows. Check which of its arguments decide which copy survives and what happens to the index, and confirm that your call returns a new object.
```
### What a Strong Answer Covers
- Compares rows across every column with no subset and keeps the first copy of each duplicate group.
- Rebuilds the index as a fresh 0 to n-1 range after rows are removed, without adding an extra column.
- Leaves the input DataFrame unchanged and keeps its columns and dtypes.
- Explains how missing values, index labels and unhashable cell values affect the comparison.
- States the time and memory cost of the deduplication pass.
### Follow-up Questions
- How would you change the function to keep the last occurrence instead, or to drop every row that has any duplicate?
- How would you deduplicate on a subset of key columns, and how would you choose which copy to keep when the other columns differ?
- Two rows differ only by letter case or trailing whitespace in a string column. How would you treat them as duplicates while still returning the original values?
- The table has 2 billion rows and does not fit in memory. How would you remove exact duplicates, and what does "first occurrence" mean there?
Overview: Write a pandas function that drops rows identical across every column, keeps the first copy, resets the index and leaves the input DataFrame untouched. Tests how well you know pandas deduplication, index handling and edge cases such as missing values and unhashable cells.
Implement def remove_exact_duplicates(df: pd.DataFrame) -> pd.DataFrame that drops rows identical across all columns, keeping the first occurrence. Return a new DataFrame.
Requirements:
Compare rows on all columns (no subset) and keep the first occurrence.
Reset the index to a fresh
0..n-1
RangeIndex (
ignore_index=True
).
Preserve the same columns and dtypes.
Do not mutate the input
df
.
Example:
df = pd.DataFrame({"a": [1, 1, 2], "b": ["x", "x", "y"]})
remove_exact_duplicates(df)
# a b
# 0 1 x
# 1 2 y
What a Strong Answer Covers Guidance
Compares rows across every column with no subset and keeps the first copy of each duplicate group.
Rebuilds the index as a fresh 0 to n-1 range after rows are removed, without adding an extra column.
Leaves the input DataFrame unchanged and keeps its columns and dtypes.
Explains how missing values, index labels and unhashable cell values affect the comparison.
States the time and memory cost of the deduplication pass.
Follow-up Questions Guidance
How would you change the function to keep the last occurrence instead, or to drop every row that has any duplicate?
How would you deduplicate on a subset of key columns, and how would you choose which copy to keep when the other columns differ?
Two rows differ only by letter case or trailing whitespace in a string column. How would you treat them as duplicates while still returning the original values?
The table has 2 billion rows and does not fit in memory. How would you remove exact duplicates, and what does "first occurrence" mean there?