Object-column cardinality report in pandas: count distinct values and flag high-cardinality columns

Quick Overview

Write a pandas function that profiles every object-dtype column with its count of distinct non-null values, the percentage of unique values over all rows, and a high-cardinality flag. It tests dtype selection, nunique, exact output dtypes and index naming, and a sort with a tie-break.

Object-column cardinality report in pandas: count distinct values and flag high-cardinality columns

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `distinct_count_report(df: pd.DataFrame, threshold: int = 50) -> pd.DataFrame` profiling the cardinality of every `object`-dtype column. Return a DataFrame indexed by column name (index name `column`). The function must meet these requirements: - Only `object`-dtype columns: numeric columns must not appear. - Columns in order: `n_unique` (int64, `nunique(dropna=True)`), `pct_unique` (float64), `is_high_cardinality` (bool, True when `n_unique > threshold`). - `pct_unique = n_unique / len(df) * 100` (denominator includes nulls). - Sort by `n_unique` descending, then column name ascending. - Dtypes and column order must match exactly. For example: ```python df = pd.DataFrame({ "country": ["US", "US", "UK", None], # low cardinality, has NaN "user_id": ["a", "b", "c", "d"], # high cardinality "age": [21, 22, 23, 24], # numeric -> excluded }) distinct_count_report(df, threshold=2) # n_unique pct_unique is_high_cardinality # column # user_id 4 100.0 True # country 2 50.0 False ``` ```hint Narrow the frame first pandas can select columns by dtype before you count anything. After that, the three statistics are one vectorized call each, and the last step is getting the dtypes and the index name exactly right. ``` ### What a Strong Answer Covers - Restricting the report to `object` columns so numeric columns never appear. - Counting distinct non-null values and dividing by the full row count, nulls included. - The strict `>` comparison for the high-cardinality flag. - Exact dtypes, column order and the `column` index name, plus the sort with its name tie-break. - Edge cases such as an empty frame, a frame with no text columns, and cells that cannot be hashed. ### Follow-up Questions - How would you extend this to also flag near-constant columns (very low cardinality)? - How would this perform on a very wide frame with millions of rows? - How would the report change your encoding choice for a column such as `user_id` versus `country`?

Overview: Write a pandas function that profiles every object-dtype column with its count of distinct non-null values, the percentage of unique values over all rows, and a high-cardinality flag. It tests dtype selection, nunique, exact output dtypes and index naming, and a sort with a tie-break.

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

Implement distinct_count_report(df: pd.DataFrame, threshold: int = 50) -> pd.DataFrame profiling the cardinality of every object-dtype column. Return a DataFrame indexed by column name (index name column).

The function must meet these requirements:

  • Only object -dtype columns: numeric columns must not appear.
  • Columns in order: n_unique (int64, nunique(dropna=True) ), pct_unique (float64), is_high_cardinality (bool, True when n_unique > threshold ).
  • pct_unique = n_unique / len(df) * 100 (denominator includes nulls).
  • Sort by n_unique descending, then column name ascending.
  • Dtypes and column order must match exactly.

For example:

df = pd.DataFrame({
    "country": ["US", "US", "UK", None],   # low cardinality, has NaN
    "user_id": ["a", "b", "c", "d"],         # high cardinality
    "age": [21, 22, 23, 24],                  # numeric -> excluded
})

distinct_count_report(df, threshold=2)
#          n_unique  pct_unique  is_high_cardinality
# column
# user_id         4       100.0                 True
# country         2        50.0                False

What a Strong Answer Covers Guidance

  • Restricting the report to object columns so numeric columns never appear.
  • Counting distinct non-null values and dividing by the full row count, nulls included.
  • The strict > comparison for the high-cardinality flag.
  • Exact dtypes, column order and the column index name, plus the sort with its name tie-break.
  • Edge cases such as an empty frame, a frame with no text columns, and cells that cannot be hashed.

Follow-up Questions Guidance

  • How would you extend this to also flag near-constant columns (very low cardinality)?
  • How would this perform on a very wide frame with millions of rows?
  • How would the report change your encoding choice for a column such as user_id versus country ?