Find column pairs that go missing together using nullity correlation in pandas

Quick Overview

Write a pandas function that finds pairs of columns that tend to be missing on the same rows by correlating their nullity masks, with a threshold and a fixed output contract. It tests isna masks, zero-variance columns, upper-triangle pair extraction, and exact sorting and dtypes.

Find column pairs that go missing together using nullity correlation in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `comissing_pairs(df: pd.DataFrame, min_corr: float) -> pd.DataFrame`, which returns the nullity correlation between pairs of columns so you can spot columns that go null on the same rows. Requirements: - Build the mask with `df.isna()`, drop constant mask columns (always null or never null), then correlate the surviving columns. - Use the strict upper triangle: list each pair once, with `col_a` before `col_b` in `df`'s column order. Keep pairs with `nullity_corr >= min_corr`. - The columns are exactly `['col_a', 'col_b', 'nullity_corr']`, with dtypes object, object and `float64`. - Sort by `nullity_corr` descending, then `col_a` ascending, then `col_b` ascending, and finish with `reset_index(drop=True)`. - If no pair survives, return an empty frame with the same columns and dtypes. Example: ```python df = pd.DataFrame({ "a": [1.0, None, 3.0, None, 5.0, 6.0], "b": ["x", None, "y", None, "z", "w"], "c": [10, 20, 30, None, None, 60], "full": [1, 2, 3, 4, 5, 6], "empty": [None] * 6, }) # 'a' and 'b' are null on identical rows (1 and 3) -> nullity_corr 1.0 # 'c' is null on rows 3 and 4, sharing one row with 'a' and 'b' # 'full' is never null and 'empty' is always null -> dropped (constant) comissing_pairs(df, 0.5) # col_a col_b nullity_corr # 0 a b 1.0 comissing_pairs(df, 0.2) # col_a col_b nullity_corr # 0 a b 1.00 # 1 a c 0.25 # 2 b c 0.25 ``` ```hint Listing pairs You need every unordered pair of surviving columns exactly once, in the order the columns appear in `df`. The standard library has a function that produces exactly that. ``` ```hint The empty case A DataFrame built from zero rows has no values to infer dtypes from. Check what dtypes your result has when no pair passes the threshold. ``` ### Constraints & Assumptions - The grader calls the function with `min_corr=0.5` on a fixed frame of about 10 rows. - The result is compared with `assert_frame_equal`, so dtypes and row order must match. ### What a Strong Answer Covers - Building the mask with `df.isna()` and dropping constant mask columns before correlating, with the reason - Each pair exactly once, oriented by `df`'s column order, with an inclusive threshold - Exact column names, dtypes, sort order and a reset index, including when the result is empty - Time and space complexity in terms of rows and surviving columns - What a high nullity correlation does and does not say about why data is missing ### Follow-up Questions - Nullity correlation can be misleading when a column is almost never null. Why? - How would you extend this to flag groups of columns that fail together, not just pairs? - Two columns have a nullity correlation of 0.95. What does that tell you about the missingness mechanism, and what would you check next?

Overview: Write a pandas function that finds pairs of columns that tend to be missing on the same rows by correlating their nullity masks, with a threshold and a fixed output contract. It tests isna masks, zero-variance columns, upper-triangle pair extraction, and exact sorting and dtypes.

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

Implement comissing_pairs(df: pd.DataFrame, min_corr: float) -> pd.DataFrame, which returns the nullity correlation between pairs of columns so you can spot columns that go null on the same rows.

Requirements:

  • Build the mask with df.isna() , drop constant mask columns (always null or never null), then correlate the surviving columns.
  • Use the strict upper triangle: list each pair once, with col_a before col_b in df 's column order. Keep pairs with nullity_corr >= min_corr .
  • The columns are exactly ['col_a', 'col_b', 'nullity_corr'] , with dtypes object, object and float64 .
  • Sort by nullity_corr descending, then col_a ascending, then col_b ascending, and finish with reset_index(drop=True) .
  • If no pair survives, return an empty frame with the same columns and dtypes.

Example:

df = pd.DataFrame({
    "a":     [1.0, None, 3.0, None, 5.0, 6.0],
    "b":     ["x", None, "y", None, "z", "w"],
    "c":     [10, 20, 30, None, None, 60],
    "full":  [1, 2, 3, 4, 5, 6],
    "empty": [None] * 6,
})
# 'a' and 'b' are null on identical rows (1 and 3)  -> nullity_corr 1.0
# 'c' is null on rows 3 and 4, sharing one row with 'a' and 'b'
# 'full' is never null and 'empty' is always null -> dropped (constant)

comissing_pairs(df, 0.5)
#   col_a col_b  nullity_corr
# 0     a     b           1.0

comissing_pairs(df, 0.2)
#   col_a col_b  nullity_corr
# 0     a     b          1.00
# 1     a     c          0.25
# 2     b     c          0.25

Constraints & Assumptions

  • The grader calls the function with min_corr=0.5 on a fixed frame of about 10 rows.
  • The result is compared with assert_frame_equal , so dtypes and row order must match.

What a Strong Answer Covers Guidance

  • Building the mask with df.isna() and dropping constant mask columns before correlating, with the reason
  • Each pair exactly once, oriented by df 's column order, with an inclusive threshold
  • Exact column names, dtypes, sort order and a reset index, including when the result is empty
  • Time and space complexity in terms of rows and surviving columns
  • What a high nullity correlation does and does not say about why data is missing

Follow-up Questions Guidance

  • Nullity correlation can be misleading when a column is almost never null. Why?
  • How would you extend this to flag groups of columns that fail together, not just pairs?
  • Two columns have a nullity correlation of 0.95. What does that tell you about the missingness mechanism, and what would you check next?