Use .corr to get the correlation between two columns
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In pandas, the simplest way to get the correlation between two columns is to call .corr() on one Series and pass the other Series as the argument. By default, this calculates Pearson correlation, which measures linear association between the two numeric columns.
Basic Usage
Suppose you have a DataFrame with two numeric columns:
This returns a number between -1 and 1.
- near
1means a strong positive linear relationship - near
0means little linear relationship - near
-1means a strong negative linear relationship
What .corr() Uses by Default
The default method is Pearson correlation.
You can also request other methods:
Use Spearman or Kendall when rank-based association makes more sense than strict linear correlation.
Missing Values Matter
Pandas automatically aligns the two Series by index and ignores pairs with missing values.
That behavior is convenient, but you should still know it is happening. Correlation results can shift if many rows are dropped because of missing values.
Correlation of Multiple Columns
If you want more than one pair, use DataFrame.corr().
That gives a correlation matrix for all numeric columns at once. For a single pair, though, the Series-to-Series form is usually clearer.
Interpret Carefully
Correlation does not imply causation, and Pearson correlation only measures linear association. Two columns can have a strong non-linear relationship while Pearson correlation looks weak.
So .corr() is a good first descriptive statistic, but not the end of the analysis.
Index Alignment Is a Feature, Not a Detail
Series correlation in pandas aligns values by index before computing the statistic. That is usually helpful, but it can surprise people who expect pure positional comparison. If the indexes differ, the effective paired values may not be the rows you thought you were comparing.
Clean Column Types First
If numeric data arrived as strings, object dtype columns can quietly block or distort analysis. Converting the columns explicitly with pd.to_numeric(..., errors='coerce') before calling .corr() is often the difference between a meaningful statistic and a confusing result.
Visualization Helps Validate the Number
A quick scatter plot is often worth generating after computing the correlation. It helps confirm whether the number matches the visible relationship and whether outliers or non-linear structure might be misleading the statistic.
That extra visual check helps keep the interpretation honest, especially when a single coefficient looks more precise than the underlying relationship really is.
Row Filtering Changes the Answer
If you subset the DataFrame before calling .corr(), the statistic describes only that filtered population. That sounds obvious, but it matters in exploratory analysis because correlations can change significantly across groups, time ranges, or cleaned subsets of the same dataset.
Common Pitfalls
- Calling
.corr()on non-numeric columns and expecting a meaningful numeric result. - Forgetting that Pearson correlation is only about linear relationships.
- Ignoring missing values and not realizing pandas dropped row pairs automatically.
- Interpreting a high correlation as proof of causation.
- Using a full correlation matrix when the analysis really only needs one clear pairwise comparison.
Summary
- Use
df['col1'].corr(df['col2'])for a simple pairwise correlation in pandas. - The default method is Pearson correlation.
- Spearman and Kendall are available when rank-based measures fit better.
- Missing values are dropped pairwise during the calculation.
- Treat correlation as a descriptive signal, not a causal conclusion.

