Count Duplicate Rows in a pandas DataFrame

Quick Overview

Count how many rows of a pandas DataFrame repeat an earlier row across every column, so a group of k identical rows adds k minus 1. It tests duplicate detection, the keep argument, NaN matching, and returning a native Python int.

Count Duplicate Rows in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `count_duplicate_rows`, which returns how many rows are exact duplicates (across all columns) of an earlier row. Each group of $k$ identical rows contributes $k - 1$, which matches `df.duplicated().sum()`. ```python def count_duplicate_rows(df: pd.DataFrame) -> int: ... ``` Requirements: - Return a native Python `int`. A NumPy scalar such as `numpy.int64` does not count. - An empty DataFrame returns `0`. - Do not mutate `df`. Example: ```python df = pd.DataFrame({"a": [1, 1, 2, 1], "b": ["x", "x", "y", "x"]}) count_duplicate_rows(df) # -> 2 (rows 1 and 3 duplicate row 0) ``` ```hint Which copies count Decide which member of each group of identical rows is the original and count only the others. Then check the type your expression returns. ``` ### What a Strong Answer Covers - Comparing rows across all columns and counting $k - 1$ per group - Getting a native `int` out of a pandas reduction - What the `keep` argument changes - How missing values and the index affect which rows match ### Follow-up Questions - What would `df.duplicated(keep=False).sum()` return on the example, and when is that the number you want? - Does a row with `NaN` in the same column as an earlier, otherwise identical row count as a duplicate? - How would you count duplicates on a subset of key columns, such as a user ID and a timestamp?

Overview: Count how many rows of a pandas DataFrame repeat an earlier row across every column, so a group of k identical rows adds k minus 1. It tests duplicate detection, the keep argument, NaN matching, and returning a native Python int.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement count_duplicate_rows, which returns how many rows are exact duplicates (across all columns) of an earlier row. Each group of kk identical rows contributes k−1k - 1, which matches df.duplicated().sum().

def count_duplicate_rows(df: pd.DataFrame) -> int:
    ...

Requirements:

  • Return a native Python int . A NumPy scalar such as numpy.int64 does not count.
  • An empty DataFrame returns 0 .
  • Do not mutate df .

Example:

df = pd.DataFrame({"a": [1, 1, 2, 1], "b": ["x", "x", "y", "x"]})
count_duplicate_rows(df)  # -> 2  (rows 1 and 3 duplicate row 0)

What a Strong Answer Covers Guidance

  • Comparing rows across all columns and counting k−1k - 1 per group
  • Getting a native int out of a pandas reduction
  • What the keep argument changes
  • How missing values and the index affect which rows match

Follow-up Questions Guidance

  • What would df.duplicated(keep=False).sum() return on the example, and when is that the number you want?
  • Does a row with NaN in the same column as an earlier, otherwise identical row count as a duplicate?
  • How would you count duplicates on a subset of key columns, such as a user ID and a timestamp?