Profile full-row duplicates in a pandas DataFrame as a float64 Series
Quick Overview
Write a pandas function that profiles full-row duplicates in a DataFrame and returns a float64 Series of counts and a duplicate percentage. It tests the difference between counting every member of a duplicate group and counting only redundant copies, plus building output that passes a strict Series comparison.
Profile full-row duplicates in a pandas DataFrame as a float64 Series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `duplicate_summary(df)` returning a profile of full-row duplicates in a pandas DataFrame (all columns considered, so two rows are duplicates only when every column matches). Do not modify or drop any rows.
The result must meet these requirements:
- Return a `float64` Series named `'duplicate_summary'` with exactly this index order: `['n_rows', 'n_unique_rows', 'n_duplicate_rows', 'n_extra_rows', 'pct_duplicate_rows']`.
- `n_rows`: total rows.
- `n_duplicate_rows`: all members of any duplicate group, first occurrence included (`df.duplicated(keep=False)`).
- `n_extra_rows`: redundant copies only (`df.duplicated(keep='first')`).
- `n_unique_rows` = `n_rows - n_extra_rows`.
- `pct_duplicate_rows` = `n_duplicate_rows / n_rows * 100`.
The output is compared with `assert_series_equal`, so the name, dtype, index order and values must all match exactly.
Example:
```python
# 8 rows: one row appears 3x, one row appears 2x, three unique rows
duplicate_summary(df)
# n_rows 8.0
# n_unique_rows 5.0
# n_duplicate_rows 5.0 # 3 + 2 copies
# n_extra_rows 3.0 # 2 + 1 redundant
# pct_duplicate_rows 62.5
# Name: duplicate_summary, dtype: float64
```
```hint Check the output contract
A strict Series comparison also checks dtype. Look at what type `.sum()` on a boolean Series returns, and make sure the index order does not depend on luck.
```
### What a Strong Answer Covers
- The difference between counting every member of a duplicate group and counting only the redundant copies, and which `keep` setting gives each.
- How `n_unique_rows` follows from the redundant-copy count.
- Building the Series with the exact name, index order and `float64` dtype so the strict comparison passes.
- Leaving `df` untouched (no `drop_duplicates`, no in-place edits).
- Edge cases such as an empty DataFrame and rows with missing values.
### Follow-up Questions
- How would you profile duplicates over a subset of key columns instead of full rows?
- How does pandas treat `NaN` when deciding whether two rows are duplicates, and when would you want different behavior?
- How would you also report the number of distinct duplicate groups and the size of the largest one?
- How would you compute the same profile on a table too large to load into memory?
Overview: Write a pandas function that profiles full-row duplicates in a DataFrame and returns a float64 Series of counts and a duplicate percentage. It tests the difference between counting every member of a duplicate group and counting only redundant copies, plus building output that passes a strict Series comparison.
Write duplicate_summary(df) returning a profile of full-row duplicates in a pandas DataFrame (all columns considered, so two rows are duplicates only when every column matches). Do not modify or drop any rows.
The result must meet these requirements:
Return a
float64
Series named
'duplicate_summary'
with exactly this index order:
['n_rows', 'n_unique_rows', 'n_duplicate_rows', 'n_extra_rows', 'pct_duplicate_rows']
.
n_rows
: total rows.
n_duplicate_rows
: all members of any duplicate group, first occurrence included (
df.duplicated(keep=False)
).
n_extra_rows
: redundant copies only (
df.duplicated(keep='first')
).