How do I calculate the correlation between columns in pandas?
Short answer
Use corr between two columns for a single coefficient, or on the whole frame for a matrix. It measures linear association only, ignores missing values pairwise, and does not imply causation.
The short version
Call .corr() on two columns for a single coefficient, or on a whole DataFrame for a matrix of every pair. It returns Pearson correlation by default, a number from -1 to 1 measuring how strongly two columns move together in a straight line. It measures linear association only, so a strong non-linear relationship can still show a low correlation.
Two columns
import pandas as pd
df = pd.DataFrame({
"temp": [10, 20, 30],
"sales": [5, 9, 13],
})
print(df["temp"].corr(df["sales"])) # 1.0
A value of 1 means the two rise together perfectly in a straight line. Here each 10-degree rise adds a fixed 4 to sales, which is exactly linear.
The whole correlation matrix
Calling .corr() on the frame gives every pairwise coefficient at once.
df.corr(numeric_only=True)
The diagonal is always 1 (each column with itself), and the matrix is symmetric. Pass numeric_only=True so text columns are ignored rather than causing an error.
What the number means, and does not
A coefficient near 1 or -1 indicates a strong straight-line relationship; near 0 indicates little linear relationship. Two crucial cautions. First, it only captures linear association, so a clear curved relationship can score near zero; always look at a scatter plot too. Second, correlation is not causation: two columns can move together because a third factor drives both, so a high value is a prompt to investigate, not a conclusion.
Missing values are handled pairwise
corr drops missing values pair by pair, using only the rows where both columns are present. That means different pairs in a matrix can be computed from different numbers of rows, which can mislead if some columns are mostly empty. Check how much data underlies each figure before trusting it.
Other correlation methods
The default is Pearson, which assumes a roughly linear relationship and is sensitive to outliers. For ranked or non-linear monotonic relationships, use Spearman, which correlates the ranks instead.
df["temp"].corr(df["sales"], method="spearman")
Spearman is more robust to outliers and captures "as one goes up, the other goes up" even when the relationship is curved, so it is a good second look when Pearson seems not to match what a scatter plot shows.
Enjoying these?
Go from piecing together answers to real fluency.
Our courses take you from looking things up to doing the work with confidence, step by step on real data.
Browse the courses