Profile the categorical columns of a pandas DataFrame: levels, top value and missing share

Quick Overview

Write a pandas function that profiles every text and category column in a DataFrame, reporting distinct levels, the most frequent value, its share of non-null rows and the percentage of missing values. It tests selecting columns by dtype, choosing the right denominators and returning an exact output schema.

Profile the categorical columns of a pandas DataFrame: levels, top value and missing share

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write a function `categorical_profile(df: pd.DataFrame) -> pd.DataFrame` that summarizes every `object`, `string` or `category` column in `df` and leaves numeric columns out. For each of those columns, report its level count, its dominant value and how complete it is. The result must have this shape: - One row per profiled column. The index holds the column names, is named `'column'`, and is sorted ascending. - The columns are exactly `['nunique', 'top', 'freq', 'top_pct', 'missing_pct']`, in that order. - `nunique` (`int64`) is the number of distinct non-null levels. - `top` (`object`) is the most frequent non-null value, and `freq` (`int64`) is its count. - `top_pct` (`float64`) is `100 * freq / (non-null count)`, rounded to 2 decimal places. - `missing_pct` (`float64`) is `100 * nulls / len(df)`, rounded to 2 decimal places. ```hint Choosing the columns pandas can filter a frame's columns by dtype in one call. Check that your filter catches all three non-numeric kinds named in the task. ``` ```hint Two different shares `top_pct` and `missing_pct` are percentages of different sets of rows. Work out which rows each one is a share of before you divide. ``` ### Constraints & Assumptions - The test frame has 10 rows: two `object` columns (one with nulls, one dominated by a single level), one `category` column, and numeric columns that must not appear in the output. - The test frame has no ties for the most frequent value, so `top` is unambiguous. - Dtypes are checked exactly: the count fields must be `int64` and the percentages `float64`. ### What a Strong Answer Covers - Selects `object`, `string` and `category` columns by dtype and excludes every numeric column. - Uses the non-null count as the denominator for `top_pct` and the full row count for `missing_pct`, both rounded to 2 decimals. - Takes the mode from non-null values only, so a null can never be reported as `top`. - Returns the exact schema: column order, the `int64`/`object`/`float64` dtypes, and an index named `'column'` sorted ascending. - Says what the function does with a modal tie or an all-null column. ### Follow-up Questions - How would you break modal ties deterministically if they occurred? - What should the row look like for a column that is entirely null, and how do you keep `freq` as `int64`? - A `category` column declares categories that never appear in the data. Does that change `nunique`, `top` or `freq`? - How would you compute the same profile for a frame too large to fit in memory?

Overview: Write a pandas function that profiles every text and category column in a DataFrame, reporting distinct levels, the most frequent value, its share of non-null rows and the percentage of missing values. It tests selecting columns by dtype, choosing the right denominators and returning an exact output schema.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write a function categorical_profile(df: pd.DataFrame) -> pd.DataFrame that summarizes every object, string or category column in df and leaves numeric columns out. For each of those columns, report its level count, its dominant value and how complete it is.

The result must have this shape:

  • One row per profiled column. The index holds the column names, is named 'column' , and is sorted ascending.
  • The columns are exactly ['nunique', 'top', 'freq', 'top_pct', 'missing_pct'] , in that order.
  • nunique ( int64 ) is the number of distinct non-null levels.
  • top ( object ) is the most frequent non-null value, and freq ( int64 ) is its count.
  • top_pct ( float64 ) is 100 * freq / (non-null count) , rounded to 2 decimal places.
  • missing_pct ( float64 ) is 100 * nulls / len(df) , rounded to 2 decimal places.

Constraints & Assumptions

  • The test frame has 10 rows: two object columns (one with nulls, one dominated by a single level), one category column, and numeric columns that must not appear in the output.
  • The test frame has no ties for the most frequent value, so top is unambiguous.
  • Dtypes are checked exactly: the count fields must be int64 and the percentages float64 .

What a Strong Answer Covers Guidance

  • Selects object , string and category columns by dtype and excludes every numeric column.
  • Uses the non-null count as the denominator for top_pct and the full row count for missing_pct , both rounded to 2 decimals.
  • Takes the mode from non-null values only, so a null can never be reported as top .
  • Returns the exact schema: column order, the int64 / object / float64 dtypes, and an index named 'column' sorted ascending.
  • Says what the function does with a modal tie or an all-null column.

Follow-up Questions Guidance

  • How would you break modal ties deterministically if they occurred?
  • What should the row look like for a column that is entirely null, and how do you keep freq as int64 ?
  • A category column declares categories that never appear in the data. Does that change nunique , top or freq ?
  • How would you compute the same profile for a frame too large to fit in memory?