Conditional Missingness Lift for Every Ordered Pair of Columns in pandas

Quick Overview

Write a pandas function that measures, for every ordered pair of columns, how much more often one column is null when the other is null, and keeps the pairs above a lift threshold. It tests null-mask arithmetic, exact output dtypes and sorting, and reasoning about which missingness measures depend on direction.

Conditional Missingness Lift for Every Ordered Pair of Columns in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `conditional_missing_lift(df, min_lift)`. For every ordered pair `(a, b)` of distinct columns, compute how often `b` is null among the rows where `a` is null, `b`'s null rate over all rows, and the ratio of the two, then filter and sort. `(a, b)` and `(b, a)` are separate rows of the output. Requirements: - `p_b_null_given_a_null` is the fraction of rows with `a` null where `b` is also null. `p_b_null_baseline` is the fraction of all rows with `b` null. Both are float64 on a 0.0 to 1.0 scale. - `lift = p_b_null_given_a_null / p_b_null_baseline` (float64). Keep only pairs with `lift >= min_lift`. - Omit pairs where `a` or `b` has zero nulls, so the output never contains `inf` or `NaN`. - Return exactly the columns `['col_a', 'col_b', 'p_b_null_given_a_null', 'p_b_null_baseline', 'lift']`. - Sort by `lift` descending, then `col_a`, then `col_b` ascending, then `reset_index(drop=True)`. Example: ```python nan = float("nan") df = pd.DataFrame({ "x": [nan, nan, 1, 1, 1, 1, 1, 1, 1, 1], "y": [nan, nan, nan, nan, 1, 1, 1, 1, 1, 1], "z": [1, 1, 1, nan, nan, nan, nan, nan, 1, 1], "v": [1, 1, 1, 1, 1, nan, nan, nan, 1, 1], "w": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], }) conditional_missing_lift(df, min_lift=1.5) # col_a col_b p_b_null_given_a_null p_b_null_baseline lift # 0 x y 1.0 0.4 2.5 # 1 y x 0.5 0.2 2.5 # 2 v z 1.0 0.5 2.0 # 3 z v 0.6 0.3 2.0 ``` Every pair involving `w` is omitted because `w` has no nulls. The other eight ordered pairs among `x`, `y`, `z` and `v` have a lift of 0.0 or 0.5 and fall below the threshold. ```hint Work from one mask Every quantity here is a count over a boolean null mask. Build it once and use its column sums and row filters. ``` ```hint Empty result When no pair passes `min_lift`, the frame still needs float64 rate columns. Check which dtypes `pd.DataFrame([], columns=[...])` gives you. ``` ### Constraints & Assumptions - Column names are unique. - A null is any value `isna()` flags. - The reference test calls the function with `min_lift=1.5`. - The result is compared with `assert_frame_equal`, so column names, dtypes, order and index must match exactly. ### What a Strong Answer Covers - Building the null mask once and deriving the conditional rate, the baseline and the lift from it. - Skipping pairs where either column has no nulls, so no `inf` or `NaN` appears. - The exact columns, float64 dtypes, sort order and index, including when no pair passes. - Which output columns depend on the direction of the pair, and whether the lift does. - The cost in rows and columns, and when the pair loop needs vectorizing. ### Follow-up Questions - How would you scale this to hundreds of columns without a Python loop over pairs? - A high lift can still be noise when `a` is rarely null. How would you guard against that? - In the example, `(x, y)` and `(y, x)` have the same lift. Is that a coincidence, and what would you report to show which column's missingness is nested inside the other's?

Overview: Write a pandas function that measures, for every ordered pair of columns, how much more often one column is null when the other is null, and keeps the pairs above a lift threshold. It tests null-mask arithmetic, exact output dtypes and sorting, and reasoning about which missingness measures depend on direction.

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

Write conditional_missing_lift(df, min_lift). For every ordered pair (a, b) of distinct columns, compute how often b is null among the rows where a is null, b's null rate over all rows, and the ratio of the two, then filter and sort. (a, b) and (b, a) are separate rows of the output.

Requirements:

  • p_b_null_given_a_null is the fraction of rows with a null where b is also null. p_b_null_baseline is the fraction of all rows with b null. Both are float64 on a 0.0 to 1.0 scale.
  • lift = p_b_null_given_a_null / p_b_null_baseline (float64). Keep only pairs with lift >= min_lift .
  • Omit pairs where a or b has zero nulls, so the output never contains inf or NaN .
  • Return exactly the columns ['col_a', 'col_b', 'p_b_null_given_a_null', 'p_b_null_baseline', 'lift'] .
  • Sort by lift descending, then col_a , then col_b ascending, then reset_index(drop=True) .

Example:

nan = float("nan")
df = pd.DataFrame({
    "x": [nan, nan, 1, 1, 1, 1, 1, 1, 1, 1],
    "y": [nan, nan, nan, nan, 1, 1, 1, 1, 1, 1],
    "z": [1, 1, 1, nan, nan, nan, nan, nan, 1, 1],
    "v": [1, 1, 1, 1, 1, nan, nan, nan, 1, 1],
    "w": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
})
conditional_missing_lift(df, min_lift=1.5)
#   col_a col_b  p_b_null_given_a_null  p_b_null_baseline  lift
# 0     x     y                    1.0                0.4   2.5
# 1     y     x                    0.5                0.2   2.5
# 2     v     z                    1.0                0.5   2.0
# 3     z     v                    0.6                0.3   2.0

Every pair involving w is omitted because w has no nulls. The other eight ordered pairs among x, y, z and v have a lift of 0.0 or 0.5 and fall below the threshold.

Constraints & Assumptions

  • Column names are unique.
  • A null is any value isna() flags.
  • The reference test calls the function with min_lift=1.5 .
  • The result is compared with assert_frame_equal , so column names, dtypes, order and index must match exactly.

What a Strong Answer Covers Guidance

  • Building the null mask once and deriving the conditional rate, the baseline and the lift from it.
  • Skipping pairs where either column has no nulls, so no inf or NaN appears.
  • The exact columns, float64 dtypes, sort order and index, including when no pair passes.
  • Which output columns depend on the direction of the pair, and whether the lift does.
  • The cost in rows and columns, and when the pair loop needs vectorizing.

Follow-up Questions Guidance

  • How would you scale this to hundreds of columns without a Python loop over pairs?
  • A high lift can still be noise when a is rarely null. How would you guard against that?
  • In the example, (x, y) and (y, x) have the same lift. Is that a coincidence, and what would you report to show which column's missingness is nested inside the other's?