Build a ranked missingness report for every column of a pandas DataFrame

Quick Overview

Write a pandas function that reports the count and percentage of missing values for every column of a DataFrame, ranked from most to least missing. It tests per-column aggregation, an exact output schema, a deterministic tie-break and edge cases such as empty frames and duplicate column names.

Build a ranked missingness report for every column of a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `missing_report(df)` returning a DataFrame that ranks every column of `df` by missingness. - One row per column (including fully populated ones), with columns in the order `['column', 'n_missing', 'pct_missing', 'dtype']`. - `column`: the column name (object). `n_missing`: `int64` count of NA cells. `dtype`: `str(df[c].dtype)` (object). - `pct_missing`: `float64` percentage of the column's cells that are NA (0-100 scale), rounded to 2 decimals. - Sort by `pct_missing` descending, ties by `column` ascending, then `reset_index(drop=True)`. - Do not drop, fill or mutate `df`. ```python df = pd.DataFrame({ 'a': [1.0, None, 3.0, None], # 2 missing 'b': ['x', 'y', 'z', 'w'], # 0 missing 'c': [None, None, None, None] # 4 missing }) missing_report(df) # column n_missing pct_missing dtype # 0 c 4 100.0 object # 1 a 2 50.0 float64 # 2 b 0 0.0 object ``` The `dtype` shown for `b` is from pandas 2.x. Pandas versions that use the dedicated string dtype by default (3.0 and later) report `str` for that column. ```hint One mask, two numbers Pandas can build a boolean mask of the missing cells for the whole frame. Think about what summing it and averaging it give you per column. ``` ### Constraints & Assumptions - NA means what `isna()` reports: `None`, `NaN`, `NaT` and `pd.NA`. Empty strings and sentinel values such as -1 are not NA. - Column names can be compared with each other (for example, all strings), so the name tie-break is well defined. ### What a Strong Answer Covers - Every column appears, including columns with no missing values and columns that are entirely missing. - The exact schema: column order, `int64` and `float64` types, and object columns for the names and dtype strings. - The two-key sort with a reset index, without touching `df`. - Edge cases such as an empty frame, duplicate column names and ties created by rounding. ### Follow-up Questions - How would you add a column showing the count of distinct non-null values? - How would you scale this to a very wide frame with millions of rows? - Some columns encode missing values as sentinels such as an empty string, -1 or "N/A". How would you include those in the report?

Overview: Write a pandas function that reports the count and percentage of missing values for every column of a DataFrame, ranked from most to least missing. It tests per-column aggregation, an exact output schema, a deterministic tie-break and edge cases such as empty frames and duplicate column names.

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

Write missing_report(df) returning a DataFrame that ranks every column of df by missingness.

  • One row per column (including fully populated ones), with columns in the order ['column', 'n_missing', 'pct_missing', 'dtype'] .
  • column : the column name (object). n_missing : int64 count of NA cells. dtype : str(df[c].dtype) (object).
  • pct_missing : float64 percentage of the column's cells that are NA (0-100 scale), rounded to 2 decimals.
  • Sort by pct_missing descending, ties by column ascending, then reset_index(drop=True) .
  • Do not drop, fill or mutate df .
df = pd.DataFrame({
    'a': [1.0, None, 3.0, None],  # 2 missing
    'b': ['x', 'y', 'z', 'w'],    # 0 missing
    'c': [None, None, None, None] # 4 missing
})
missing_report(df)
#   column  n_missing  pct_missing    dtype
# 0      c          4        100.0   object
# 1      a          2         50.0  float64
# 2      b          0          0.0   object

The dtype shown for b is from pandas 2.x. Pandas versions that use the dedicated string dtype by default (3.0 and later) report str for that column.

Constraints & Assumptions

  • NA means what isna() reports: None , NaN , NaT and pd.NA . Empty strings and sentinel values such as -1 are not NA.
  • Column names can be compared with each other (for example, all strings), so the name tie-break is well defined.

What a Strong Answer Covers Guidance

  • Every column appears, including columns with no missing values and columns that are entirely missing.
  • The exact schema: column order, int64 and float64 types, and object columns for the names and dtype strings.
  • The two-key sort with a reset index, without touching df .
  • Edge cases such as an empty frame, duplicate column names and ties created by rounding.

Follow-up Questions Guidance

  • How would you add a column showing the count of distinct non-null values?
  • How would you scale this to a very wide frame with millions of rows?
  • Some columns encode missing values as sentinels such as an empty string, -1 or "N/A". How would you include those in the report?