Report highly correlated numeric column pairs in a pandas DataFrame

Quick Overview

Write a pandas function that lists every pair of numeric columns whose absolute Pearson correlation is above a threshold, with a fixed output schema and sort order. It tests correlation matrices, visiting each column pair once, and careful handling of dtypes and empty results.

Report highly correlated numeric column pairs in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write a Python function `high_correlation_report(df, threshold)` that flags every pair of distinct numeric columns in a pandas DataFrame whose absolute Pearson correlation is strictly greater than `threshold`. Return a `pd.DataFrame` with one row per flagged pair. The output must follow these rules: - Use numeric columns only, meaning the columns `df.select_dtypes(include="number")` returns. Boolean columns do not count as numeric. - Exclude self-pairs and report each unordered pair once, with `col_a` and `col_b` in the column order of the correlation matrix. - Flag a pair when `abs(r) > threshold`. - The columns are exactly `['col_a', 'col_b', 'r', 'direction']`, with dtypes `object, object, float64, object`. - `direction` is `'positive'` if `r > 0`, else `'negative'`. - Sort by absolute `r` descending, then `col_a` ascending, then `col_b` ascending, and finish with `reset_index(drop=True)`. - If no pair qualifies, return an empty DataFrame with the same four columns and the same dtypes. Example: ```text df has columns: age, income, debt corr(age, income) = 0.91 corr(age, debt) = -0.85 corr(income, debt) = 0.40 high_correlation_report(df, 0.8) col_a col_b r direction 0 age income 0.91 positive 1 age debt -0.85 negative ``` The pair (income, debt) is left out because 0.40 is below the threshold, and (age, income) comes first because its absolute correlation is larger. ```hint Visiting each pair once The correlation matrix is symmetric and has ones on its diagonal. Work out which cells you actually need to read. ``` ```hint The empty case Check what dtype pandas gives each column of a DataFrame built from zero rows. ``` ### What a Strong Answer Covers - Restricting the computation to numeric columns, and what happens when the frame has fewer than two of them - Reporting each unordered pair exactly once, with no self-pairs and the columns in matrix order - Applying the strict threshold and the full sort order (absolute `r`, then `col_a`, then `col_b`) - Returning the exact column names and dtypes in both the populated and the empty case - Handling pairs whose correlation is `NaN`, and the cost of the computation as the column count grows ### Follow-up Questions - How would you handle columns that are constant or contain many `NaN` values? - How would you group correlated features into clusters instead of listing pairs? - When would you switch this report from Pearson to Spearman correlation, and what changes in the code?

Overview: Write a pandas function that lists every pair of numeric columns whose absolute Pearson correlation is above a threshold, with a fixed output schema and sort order. It tests correlation matrices, visiting each column pair once, and careful handling of dtypes and empty results.

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

Write a Python function high_correlation_report(df, threshold) that flags every pair of distinct numeric columns in a pandas DataFrame whose absolute Pearson correlation is strictly greater than threshold. Return a pd.DataFrame with one row per flagged pair.

The output must follow these rules:

  • Use numeric columns only, meaning the columns df.select_dtypes(include="number") returns. Boolean columns do not count as numeric.
  • Exclude self-pairs and report each unordered pair once, with col_a and col_b in the column order of the correlation matrix.
  • Flag a pair when abs(r) > threshold .
  • The columns are exactly ['col_a', 'col_b', 'r', 'direction'] , with dtypes object, object, float64, object .
  • direction is 'positive' if r > 0 , else 'negative' .
  • Sort by absolute r descending, then col_a ascending, then col_b ascending, and finish with reset_index(drop=True) .
  • If no pair qualifies, return an empty DataFrame with the same four columns and the same dtypes.

Example:

df has columns: age, income, debt
corr(age, income)  =  0.91
corr(age, debt)    = -0.85
corr(income, debt) =  0.40

high_correlation_report(df, 0.8)

  col_a   col_b      r  direction
0   age  income   0.91   positive
1   age    debt  -0.85   negative

The pair (income, debt) is left out because 0.40 is below the threshold, and (age, income) comes first because its absolute correlation is larger.

What a Strong Answer Covers Guidance

  • Restricting the computation to numeric columns, and what happens when the frame has fewer than two of them
  • Reporting each unordered pair exactly once, with no self-pairs and the columns in matrix order
  • Applying the strict threshold and the full sort order (absolute r , then col_a , then col_b )
  • Returning the exact column names and dtypes in both the populated and the empty case
  • Handling pairs whose correlation is NaN , and the cost of the computation as the column count grows

Follow-up Questions Guidance

  • How would you handle columns that are constant or contain many NaN values?
  • How would you group correlated features into clusters instead of listing pairs?
  • When would you switch this report from Pearson to Spearman correlation, and what changes in the code?