Group DataFrame rows by their missing-value pattern in pandas

Quick Overview

Write a pandas function that groups DataFrame rows by which columns are null and reports each pattern's null count, row count and share of rows. It tests boolean null masks, groupby aggregation, deterministic multi-key sorting and exact output dtypes.

Group DataFrame rows by their missing-value pattern in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write a function `missingness_pattern_report(df)` that groups the rows of a pandas DataFrame by their nullity pattern, meaning the set of columns that are null in that row. Return a DataFrame with one row per distinct pattern and exactly the columns `['pattern', 'n_missing_cols', 'n_rows', 'pct_rows']`. Do not drop or fill any pattern group. The output must follow these rules: - `pattern` (object) is the comma-joined names of the null columns, in `df`'s original column order, with no spaces (for example `'age,income'`). - Rows with no nulls get the pattern `'(complete)'` and an `n_missing_cols` of 0. - `n_missing_cols` (int64) is the number of null columns in the pattern, and `n_rows` (int64) is the number of rows that share it. - `pct_rows` (float64) is `n_rows / len(df) * 100`. - Sort by `n_rows` descending, then `pattern` ascending, then call `reset_index(drop=True)`. For example, given this frame: ```python df = pd.DataFrame({ "age": [31, None, 45, None, 28], "income": [72000, None, 58000, None, 61000], "email": ["a@x.com", "b@x.com", None, "d@x.com", "e@x.com"], "phone": ["555-0101", "555-0102", "555-0103", "555-0104", None], }) ``` the function returns: ```text pattern n_missing_cols n_rows pct_rows 0 age,income 2 2 40.0 1 (complete) 0 1 20.0 2 email 1 1 20.0 3 phone 1 1 20.0 ``` Rows 1 and 3 share the `age,income` pattern, so it comes first. The other three patterns tie at one row each and fall back to ascending `pattern` order, where `'(complete)'` sorts first because `(` comes before any letter. ```hint Build a key per row Turn each row of the boolean null mask into one string. After that the task is an ordinary group-and-count. ``` ```hint Watch the dtypes The output is compared with exact dtypes. Check what your pattern and count columns end up as after the groupby, and what happens on an input with zero rows. ``` ### Clarifying Questions to Ask - Should only values that `isna()` flags count as missing, or also empty strings and sentinel codes such as -999? - What should the function return for a DataFrame with zero rows? - Are column names guaranteed to be unique strings, or can they be integers, repeat, or contain commas? ### What a Strong Answer Covers - Builds the pattern string in the original column order, with the `'(complete)'` case for rows that have no nulls. - Counts rows per pattern without dropping any group, including single-row patterns. - Applies the two-key sort and resets the index so the row order is deterministic. - Returns the exact column order and dtypes (object, int64, int64, float64). - Handles edge cases such as an empty frame, non-string column names and column names that contain commas. ### Follow-up Questions - How would you flag which patterns look structural (a shared cause) and which look like random missingness? - Why sort by `pattern` ascending as a tie-breaker instead of leaving the order of tied patterns arbitrary? - How would you make this fast on a frame with tens of millions of rows and a few hundred columns? - How would this report change your choice between dropping rows, imputing values and adding missingness indicator features?

Overview: Write a pandas function that groups DataFrame rows by which columns are null and reports each pattern's null count, row count and share of rows. It tests boolean null masks, groupby aggregation, deterministic multi-key sorting and exact output dtypes.

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

Write a function missingness_pattern_report(df) that groups the rows of a pandas DataFrame by their nullity pattern, meaning the set of columns that are null in that row. Return a DataFrame with one row per distinct pattern and exactly the columns ['pattern', 'n_missing_cols', 'n_rows', 'pct_rows']. Do not drop or fill any pattern group.

The output must follow these rules:

  • pattern (object) is the comma-joined names of the null columns, in df 's original column order, with no spaces (for example 'age,income' ).
  • Rows with no nulls get the pattern '(complete)' and an n_missing_cols of 0.
  • n_missing_cols (int64) is the number of null columns in the pattern, and n_rows (int64) is the number of rows that share it.
  • pct_rows (float64) is n_rows / len(df) * 100 .
  • Sort by n_rows descending, then pattern ascending, then call reset_index(drop=True) .

For example, given this frame:

df = pd.DataFrame({
    "age":    [31, None, 45, None, 28],
    "income": [72000, None, 58000, None, 61000],
    "email":  ["a@x.com", "b@x.com", None, "d@x.com", "e@x.com"],
    "phone":  ["555-0101", "555-0102", "555-0103", "555-0104", None],
})

the function returns:

      pattern  n_missing_cols  n_rows  pct_rows
0  age,income               2       2      40.0
1  (complete)               0       1      20.0
2       email               1       1      20.0
3       phone               1       1      20.0

Rows 1 and 3 share the age,income pattern, so it comes first. The other three patterns tie at one row each and fall back to ascending pattern order, where '(complete)' sorts first because ( comes before any letter.

Clarifying Questions to Ask Guidance

  • Should only values that isna() flags count as missing, or also empty strings and sentinel codes such as -999?
  • What should the function return for a DataFrame with zero rows?
  • Are column names guaranteed to be unique strings, or can they be integers, repeat, or contain commas?

What a Strong Answer Covers Guidance

  • Builds the pattern string in the original column order, with the '(complete)' case for rows that have no nulls.
  • Counts rows per pattern without dropping any group, including single-row patterns.
  • Applies the two-key sort and resets the index so the row order is deterministic.
  • Returns the exact column order and dtypes (object, int64, int64, float64).
  • Handles edge cases such as an empty frame, non-string column names and column names that contain commas.

Follow-up Questions Guidance

  • How would you flag which patterns look structural (a shared cause) and which look like random missingness?
  • Why sort by pattern ascending as a tie-breaker instead of leaving the order of tied patterns arbitrary?
  • How would you make this fast on a frame with tens of millions of rows and a few hundred columns?
  • How would this report change your choice between dropping rows, imputing values and adding missingness indicator features?