Feature exploration
Explore relationships among numeric features before modeling.
Pandas · Module
Computes pairwise column correlations while excluding missing values. Supports Pearson, Kendall, Spearman, and custom callable methods.
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.
DataFrame.corr(method='pearson', min_periods=1, numeric_only=False)| Name | Type | Description |
|---|---|---|
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_periods | int, default 1 | Minimum observations required for a valid pairwise result; supported for Pearson and Spearman. |
numeric_only | bool, default False | Whether to include only float, integer, or boolean columns. |
DataFrame — 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.
Explore relationships among numeric features before modeling.
Compare ordered variables without assuming linear relationships.
Control whether sparse column pairs produce valid results.
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.
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.
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.corrwithSeries.corr