Write a pandas column profiler that flags constant, unique, high-cardinality and missing columns

Quick Overview

Write a pandas function that scans every DataFrame column and reports constant, unique, high-cardinality and missing-value alerts as a sorted long-form table. It tests null handling with nunique, exact output dtypes and the empty-result case.

Write a pandas column profiler that flags constant, unique, high-cardinality and missing columns

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write a function `column_alerts(df: pd.DataFrame) -> pd.DataFrame` that scans every column of a DataFrame and returns one row per (column, alert) pair, like the alert panel of an automated data profiling report. A column may raise several alerts or none. The output columns are exactly `['column', 'alert', 'value']`, with dtypes `object`, `object` and `float64`. The alert rules are: - `'constant'`: `nunique(dropna=False) <= 1`. The value is that nunique count. - `'unique'`: the column has no nulls and its distinct count equals the column length. The value is `1.0`. - `'high_cardinality'`: `nunique(dropna=True) > 50`. The value is that nunique count. - `'missing'`: the column has any nulls. The value is `100 * nulls / len(df)` rounded to 2 decimal places. Sort the result by `column`, then by `alert`, both ascending, and finish with `reset_index(drop=True)`. If no alert fires, return an empty frame with the same three columns and dtypes. ```hint Evaluate rules independently Check each rule on its own for every column and collect the matching rows in a list. Build the DataFrame once at the end. ``` ```hint The empty case Look at the dtypes pandas gives a DataFrame built from an empty list of rows. ``` ### Constraints & Assumptions - The grader uses a fixed 60-row frame. It includes a column that fires both `'unique'` and `'high_cardinality'`, and one clean column that fires nothing. - The cardinality cutoff is fixed at 50 in this version. - Counts go into `value` as floats, so the column stays `float64`. ### What a Strong Answer Covers - Each rule is checked independently, so one column can produce zero, one or several rows. - The null handling matches each rule: which counts include nulls and which exclude them. - The output has exactly the three columns with the required dtypes, including when no alert fires. - The rows are sorted by column, then alert, with a fresh index. - Complexity and edge cases such as a zero-row frame or an all-null column. ### Follow-up Questions - How would you make the alert rules configurable, for example with a tunable cardinality threshold? - How would you add a new alert type without disturbing the existing ordering? - How would you produce the same report for a table too large to load into memory?

Overview: Write a pandas function that scans every DataFrame column and reports constant, unique, high-cardinality and missing-value alerts as a sorted long-form table. It tests null handling with nunique, exact output dtypes and the empty-result case.

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

Write a function column_alerts(df: pd.DataFrame) -> pd.DataFrame that scans every column of a DataFrame and returns one row per (column, alert) pair, like the alert panel of an automated data profiling report. A column may raise several alerts or none. The output columns are exactly ['column', 'alert', 'value'], with dtypes object, object and float64.

The alert rules are:

  • 'constant' : nunique(dropna=False) <= 1 . The value is that nunique count.
  • 'unique' : the column has no nulls and its distinct count equals the column length. The value is 1.0 .
  • 'high_cardinality' : nunique(dropna=True) > 50 . The value is that nunique count.
  • 'missing' : the column has any nulls. The value is 100 * nulls / len(df) rounded to 2 decimal places.

Sort the result by column, then by alert, both ascending, and finish with reset_index(drop=True). If no alert fires, return an empty frame with the same three columns and dtypes.

Constraints & Assumptions

  • The grader uses a fixed 60-row frame. It includes a column that fires both 'unique' and 'high_cardinality' , and one clean column that fires nothing.
  • The cardinality cutoff is fixed at 50 in this version.
  • Counts go into value as floats, so the column stays float64 .

What a Strong Answer Covers Guidance

  • Each rule is checked independently, so one column can produce zero, one or several rows.
  • The null handling matches each rule: which counts include nulls and which exclude them.
  • The output has exactly the three columns with the required dtypes, including when no alert fires.
  • The rows are sorted by column, then alert, with a fresh index.
  • Complexity and edge cases such as a zero-row frame or an all-null column.

Follow-up Questions Guidance

  • How would you make the alert rules configurable, for example with a tunable cardinality threshold?
  • How would you add a new alert type without disturbing the existing ordering?
  • How would you produce the same report for a table too large to load into memory?