Return Every Duplicated Row of a pandas DataFrame, Including First Occurrences
Quick Overview
Write a pandas function that returns every row having an exact copy elsewhere in the DataFrame, keeping first occurrences and the original row order. It tests how duplicate flags treat each member of a group, how missing values compare, and how to renumber the filtered result without leaking the old index.
Return Every Duplicated Row of a pandas DataFrame, Including First Occurrences
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `only_duplicated_rows(df)` that returns a new DataFrame with only the rows that have an identical row (across all columns) elsewhere in the frame. Keep every occurrence, including the first, and drop unique rows.
```python
import pandas as pd
def only_duplicated_rows(df: pd.DataFrame) -> pd.DataFrame:
...
```
Requirements:
- Compare rows on all columns.
- Preserve the original row order.
- Reset the index to `0..n-1`, discarding the old index.
- Do not mutate the input.
Example:
```python
df = pd.DataFrame({"a": [1, 1, 2, 3], "b": ["x", "x", "y", "z"]})
only_duplicated_rows(df)
# a b
# 0 1 x
# 1 1 x
```
```hint Check the default
pandas can flag repeated rows for you. Look at how its default setting treats the first member of each group of identical rows, and whether that matches what this task keeps.
```
### What a Strong Answer Covers
- How the mask decides which members of a duplicate group are flagged
- Why the filtered result keeps the original row order and leaves the input unchanged
- How the index is renumbered without the old labels turning into a column
- How missing values and index labels affect which rows count as identical
### Follow-up Questions
- How would you find rows that are duplicates only on a subset of columns, such as a user id and a date?
- How would you return each duplicate group once, together with how many times it appears?
- How does the duplicate check treat missing values, and how would you change that?
- How would you catch near duplicates, such as text that differs only in case or surrounding whitespace?
Overview: Write a pandas function that returns every row having an exact copy elsewhere in the DataFrame, keeping first occurrences and the original row order. It tests how duplicate flags treat each member of a group, how missing values compare, and how to renumber the filtered result without leaking the old index.
Write only_duplicated_rows(df) that returns a new DataFrame with only the rows that have an identical row (across all columns) elsewhere in the frame. Keep every occurrence, including the first, and drop unique rows.
import pandas as pd
def only_duplicated_rows(df: pd.DataFrame) -> pd.DataFrame:
...
Requirements:
Compare rows on all columns.
Preserve the original row order.
Reset the index to
0..n-1
, discarding the old index.
Do not mutate the input.
Example:
df = pd.DataFrame({"a": [1, 1, 2, 3], "b": ["x", "x", "y", "z"]})
only_duplicated_rows(df)
# a b
# 0 1 x
# 1 1 x
What a Strong Answer Covers Guidance
How the mask decides which members of a duplicate group are flagged
Why the filtered result keeps the original row order and leaves the input unchanged
How the index is renumbered without the old labels turning into a column
How missing values and index labels affect which rows count as identical
Follow-up Questions Guidance
How would you find rows that are duplicates only on a subset of columns, such as a user id and a date?
How would you return each duplicate group once, together with how many times it appears?
How does the duplicate check treat missing values, and how would you change that?
How would you catch near duplicates, such as text that differs only in case or surrounding whitespace?