Count Distinct Rows in a pandas DataFrame

Quick Overview

Count the distinct rows of a pandas DataFrame, where two rows match only when every column value is equal. It tests full-row deduplication, how missing values compare, returning a native Python int, and why some counting shortcuts disagree on data with NaN.

Count Distinct Rows in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `num_unique_rows`, which returns the number of distinct rows in a DataFrame. Two rows are duplicates when every column value matches. ```python def num_unique_rows(df: pd.DataFrame) -> int: ... ``` Requirements: - The result equals `int(len(df.drop_duplicates()))`, a full-row deduplication with `keep='first'`. - Return a plain Python `int`. A NumPy integer type does not satisfy this. - An empty DataFrame returns `0`. - Do not mutate `df`. Example: ```python df = pd.DataFrame({ "a": [1, 1, 2, 1], "b": ["x", "x", "y", "x"], }) num_unique_rows(df) # -> 2 (rows (1, 'x') and (2, 'y')) ``` ```hint Compare whole rows Treat each row as one tuple of all its column values, and check what pandas already offers for finding repeated rows. ``` ### What a Strong Answer Covers - Comparing rows on all columns at once - Returning a native Python `int` and handling the empty DataFrame - How missing values compare when deciding that two rows match - The time and memory cost on a large frame ### Follow-up Questions - How would you count distinct rows on a subset of key columns only? - Does this treat `NaN` values as equal? - Why can `len(df.value_counts())` or `df.groupby(list(df.columns)).ngroups` give a different number on real data?

Overview: Count the distinct rows of a pandas DataFrame, where two rows match only when every column value is equal. It tests full-row deduplication, how missing values compare, returning a native Python int, and why some counting shortcuts disagree on data with NaN.

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

Implement num_unique_rows, which returns the number of distinct rows in a DataFrame. Two rows are duplicates when every column value matches.

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

Requirements:

  • The result equals int(len(df.drop_duplicates())) , a full-row deduplication with keep='first' .
  • Return a plain Python int . A NumPy integer type does not satisfy this.
  • An empty DataFrame returns 0 .
  • Do not mutate df .

Example:

df = pd.DataFrame({
    "a": [1, 1, 2, 1],
    "b": ["x", "x", "y", "x"],
})
num_unique_rows(df)  # -> 2   (rows (1, 'x') and (2, 'y'))

What a Strong Answer Covers Guidance

  • Comparing rows on all columns at once
  • Returning a native Python int and handling the empty DataFrame
  • How missing values compare when deciding that two rows match
  • The time and memory cost on a large frame

Follow-up Questions Guidance

  • How would you count distinct rows on a subset of key columns only?
  • Does this treat NaN values as equal?
  • Why can len(df.value_counts()) or df.groupby(list(df.columns)).ngroups give a different number on real data?