Pairwise vs listwise correlation gap: compare pairwise-complete and complete-case Pearson r in pandas

Quick Overview

Write a pandas function that compares the pairwise-complete and complete-case (listwise) Pearson correlation for every pair of numeric columns and ranks the pairs by the absolute gap. It tests how missing-data handling changes a correlation, pair generation in column order, exact dtypes and deterministic sorting.

Pairwise vs listwise correlation gap: compare pairwise-complete and complete-case Pearson r in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `pairwise_vs_listwise_gap(df)`. For every unordered pair of distinct numeric columns, compare the pairwise-complete correlation with the complete-case (listwise) correlation and report the absolute gap. Non-numeric columns are ignored. The function must meet these requirements: - Return a `pd.DataFrame` with columns EXACTLY `['col_a', 'col_b', 'r_pairwise', 'r_listwise', 'abs_gap']` and dtypes `object, object, float64, float64, float64`. - One row per unordered pair with `col_a` before `col_b` in column order (no self-pairs). - `r_pairwise`: pandas' DEFAULT Pearson correlation (each pair uses every row where both columns are non-null). - `r_listwise`: correlation after dropping any row with a NaN in ANY numeric column. `abs_gap`: absolute difference of the two. - Sort by `abs_gap` descending, then `col_a`, then `col_b` ascending, and `reset_index(drop=True)`. ```hint Which frame, which call Both correlations can come from the same pandas method. The only difference is which rows the frame still has when you call it. ``` ```hint Pairs and gaps Generate pairs from positions in the column list so each unordered pair appears once in frame order. Then check what happens to a pair whose correlation is undefined. ``` ### What a Strong Answer Covers - Restricting to numeric columns and producing each unordered pair exactly once, in frame column order, with no self-pairs. - Computing the pairwise-complete matrix on the numeric frame and the listwise matrix after dropping every row that has a NaN in any numeric column. - The exact output columns and dtypes, including when there are fewer than two numeric columns. - The three-key sort, and where NaN correlations and gaps end up. - Why the two estimates differ and what a large gap says about the missing data. ### Follow-up Questions - How would you handle a pair whose `r_listwise` is NaN because too few rows survive `dropna()`? - Why can listwise deletion bias the correlation beyond just reducing sample size? - How would you add the number of rows behind each estimate to the report? - If the data were missing at random given the observed columns, what would you use in place of either deletion method?

Overview: Write a pandas function that compares the pairwise-complete and complete-case (listwise) Pearson correlation for every pair of numeric columns and ranks the pairs by the absolute gap. It tests how missing-data handling changes a correlation, pair generation in column order, exact dtypes and deterministic sorting.

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

Write pairwise_vs_listwise_gap(df). For every unordered pair of distinct numeric columns, compare the pairwise-complete correlation with the complete-case (listwise) correlation and report the absolute gap. Non-numeric columns are ignored.

The function must meet these requirements:

  • Return a pd.DataFrame with columns EXACTLY ['col_a', 'col_b', 'r_pairwise', 'r_listwise', 'abs_gap'] and dtypes object, object, float64, float64, float64 .
  • One row per unordered pair with col_a before col_b in column order (no self-pairs).
  • r_pairwise : pandas' DEFAULT Pearson correlation (each pair uses every row where both columns are non-null).
  • r_listwise : correlation after dropping any row with a NaN in ANY numeric column. abs_gap : absolute difference of the two.
  • Sort by abs_gap descending, then col_a , then col_b ascending, and reset_index(drop=True) .

What a Strong Answer Covers Guidance

  • Restricting to numeric columns and producing each unordered pair exactly once, in frame column order, with no self-pairs.
  • Computing the pairwise-complete matrix on the numeric frame and the listwise matrix after dropping every row that has a NaN in any numeric column.
  • The exact output columns and dtypes, including when there are fewer than two numeric columns.
  • The three-key sort, and where NaN correlations and gaps end up.
  • Why the two estimates differ and what a large gap says about the missing data.

Follow-up Questions Guidance

  • How would you handle a pair whose r_listwise is NaN because too few rows survive dropna() ?
  • Why can listwise deletion bias the correlation beyond just reducing sample size?
  • How would you add the number of rows behind each estimate to the report?
  • If the data were missing at random given the observed columns, what would you use in place of either deletion method?