Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.corr

Computes pairwise column correlations while excluding missing values. Supports Pearson, Kendall, Spearman, and custom callable methods.

How it works

DataFrame.corr computes pairwise correlations among columns after excluding missing values. It supports Pearson, Kendall, Spearman, or a callable, and can restrict computation to numeric columns.

Syntax

DataFrame.corr(method='pearson', min_periods=1, numeric_only=False)

Parameters

NameTypeDescription
method{'pearson', 'kendall', 'spearman'} or callable, default 'pearson'Correlation method: 'pearson', 'kendall', 'spearman', or a callable accepting two one-dimensional ndarrays and returning a float.
min_periodsint, default 1Minimum observations required for a valid pairwise result; supported for Pearson and Spearman.
numeric_onlybool, default FalseWhether to include only float, integer, or boolean columns.

Returns

DataFrame — correlation matrix.

Key points

  • The method builds a column-by-column correlation matrix using available nonmissing values.
  • The method argument selects the correlation algorithm or accepts a custom callable.
  • Pearson is the standard correlation coefficient and is the default method.
  • Kendall computes Kendall Tau correlation.
  • Spearman computes rank correlation.
  • Set numeric_only to include only floating-point, integer, or boolean columns.

Common mistakes

  • min_periods does not affect Kendall correlation; the documentation limits this option to Pearson and Spearman.
  • Diagonal entries are forced to 1 even when a custom callable would produce different self-correlations.

Example: Spearman correlation matrix

import pandas as pd

sales = pd.DataFrame({
    "units": [10, 20, 30, 40],
    "revenue": [100, 190, 310, 380],
    "returns": [1, 2, 1, 3],
})

print(sales.corr(method="spearman", min_periods=3))

Computes a rank-based correlation matrix while requiring at least three paired observations for each result.

When to use it

Feature exploration

Explore relationships among numeric features before modeling.

Rank correlation

Compare ordered variables without assuming linear relationships.

Minimum-observation filtering

Control whether sparse column pairs produce valid results.

Interview questions

QWhat does DataFrame.corr return?

By default, corr uses Pearson correlation and returns a DataFrame whose entries represent pairwise column correlations. Missing values are excluded from each pairwise calculation.

Follow-up: How would you require sufficient data? Pass min_periods with the minimum number of observations needed for each pair.

QWhich correlation methods can be selected?

method accepts pearson, kendall, spearman, or a callable. A callable receives two one-dimensional ndarrays and must return a float.

Follow-up: What structural properties does the result retain for a callable? Its diagonal is 1 and the matrix is symmetric regardless of callable behavior.

QWhat does numeric_only control?

numeric_only controls whether only float, integer, and boolean columns participate. Its default is False, so nonnumeric columns are not automatically excluded by this option.

Follow-up: How can you make the input explicit? Select the intended numeric columns before calling corr, especially when mixed-type data is present.

  • DataFrame.corrwith
  • Series.corr

Source: pandas.DataFrame.corr. Checked 2026-10-15.