Rank the Top N Most Correlated Pairs of Numeric Columns in pandas

Quick Overview

Write a pandas function that returns the n pairs of distinct numeric columns with the largest absolute Pearson correlation as a sorted DataFrame. It tests pulling unique pairs out of a correlation matrix, a deterministic multi-key sort, exact output dtypes, and handling undefined correlations.

Rank the Top N Most Correlated Pairs of Numeric Columns in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `top_correlated_pairs(df, n)` that returns a `pd.DataFrame` of the `n` pairs of distinct numeric columns with the largest absolute Pearson correlation. The result must follow these rules: - The columns are exactly `['col_a', 'col_b', 'r', 'abs_r']` with dtypes `object`, `object`, `float64`, `float64`. - `r` is the signed Pearson correlation and `abs_r` is its absolute value. - Only numeric columns are used; others are ignored. Self-pairs are excluded and each unordered pair appears once, with `col_a` being the column that comes earlier in `df.columns`. - Rows are sorted by `abs_r` descending, then `col_a` ascending, then `col_b` ascending, followed by `reset_index(drop=True)`. - If `n` exceeds the number of pairs, all pairs are returned. ```python df = pd.DataFrame({ "x": [1, 3, 5, 7], "y": [5, 25, 15, 35], "z": [3, -1, 5, 1], "label": ["p", "q", "r", "s"], }) top_correlated_pairs(df, n=2) ``` ```text col_a col_b r abs_r 0 x y 0.8 0.8 1 y z -0.6 0.6 ``` `label` is text, so it is ignored. The three numeric pairs have correlations 0.8 for `(x, y)`, -0.6 for `(y, z)` and 0 for `(x, z)`, so the two strongest are `(x, y)` and `(y, z)`. With `n=10` the function returns all three pairs, with `(x, z)` last. ```hint Pairs from the matrix The correlation matrix holds every pair twice, plus a diagonal of ones. Find a way to keep only the cells on one side of the diagonal. ``` ```hint Check the schema last Column names pulled out of an index do not always come back as `object` dtype. Compare your final dtypes against the required ones. ``` ### Clarifying Questions to Ask - Do boolean columns count as numeric? - Can `n` be zero or negative, and what should happen then? ### What a Strong Answer Covers - One correlation matrix computed on the numeric columns only. - Each unordered pair once, with no self-pairs and `col_a` earlier in `df.columns`. - The three-key sort, the cap at `n` rows, and a reset index. - The exact dtypes, including `object` for the two name columns. - Pairs whose correlation is undefined. ### Follow-up Questions - How would you handle columns that are constant or all-NaN? - How would you extend this to Spearman or a configurable method? - With hundreds of columns and few rows, many pairs look strong by chance. How would you decide which correlations are worth reporting?

Overview: Write a pandas function that returns the n pairs of distinct numeric columns with the largest absolute Pearson correlation as a sorted DataFrame. It tests pulling unique pairs out of a correlation matrix, a deterministic multi-key sort, exact output dtypes, and handling undefined correlations.

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

Write top_correlated_pairs(df, n) that returns a pd.DataFrame of the n pairs of distinct numeric columns with the largest absolute Pearson correlation.

The result must follow these rules:

  • The columns are exactly ['col_a', 'col_b', 'r', 'abs_r'] with dtypes object , object , float64 , float64 .
  • r is the signed Pearson correlation and abs_r is its absolute value.
  • Only numeric columns are used; others are ignored. Self-pairs are excluded and each unordered pair appears once, with col_a being the column that comes earlier in df.columns .
  • Rows are sorted by abs_r descending, then col_a ascending, then col_b ascending, followed by reset_index(drop=True) .
  • If n exceeds the number of pairs, all pairs are returned.
df = pd.DataFrame({
    "x": [1, 3, 5, 7],
    "y": [5, 25, 15, 35],
    "z": [3, -1, 5, 1],
    "label": ["p", "q", "r", "s"],
})
top_correlated_pairs(df, n=2)
  col_a col_b    r  abs_r
0     x     y  0.8    0.8
1     y     z -0.6    0.6

label is text, so it is ignored. The three numeric pairs have correlations 0.8 for (x, y), -0.6 for (y, z) and 0 for (x, z), so the two strongest are (x, y) and (y, z). With n=10 the function returns all three pairs, with (x, z) last.

Clarifying Questions to Ask Guidance

  • Do boolean columns count as numeric?
  • Can n be zero or negative, and what should happen then?

What a Strong Answer Covers Guidance

  • One correlation matrix computed on the numeric columns only.
  • Each unordered pair once, with no self-pairs and col_a earlier in df.columns .
  • The three-key sort, the cap at n rows, and a reset index.
  • The exact dtypes, including object for the two name columns.
  • Pairs whose correlation is undefined.

Follow-up Questions Guidance

  • How would you handle columns that are constant or all-NaN?
  • How would you extend this to Spearman or a configurable method?
  • With hundreds of columns and few rows, many pairs look strong by chance. How would you decide which correlations are worth reporting?