Pandas: Calculate Correlation with corr()

Compute pairwise correlation of columns in a DataFrame using the Pandas corr() method. Analyze relationships between numerical variables using Pearson, Spearman, or Kendall correlation coefficients, and manage missing data thresholds.

Introduction to corr() 🔝

The corr() method computes pairwise correlation of numerical columns, excluding missing values by default. It returns a correlation matrix where values range from -1.0 to 1.0, indicating the strength and direction of linear or monotonic relationships.

Create numerical sample DataFrame 🔝

We start with a multi-column numerical DataFrame representing study hours, attendance, and exam scores.
import pandas as pd

df = pd.DataFrame({
    "HOURS": [2, 4, 6, 8, 10],
    "ATTENDANCE": [50, 60, 75, 90, 95],
    "SCORE": [55, 65, 78, 92, 98]
})
print(df)
assert df.shape == (5, 3)

Expected output

   HOURS  ATTENDANCE  SCORE
0      2          50     55
1      4          60     65
2      6          75     78
3      8          90     92
4     10          95     98

Basic Pearson correlation matrix 🔝

Call corr() on the DataFrame to calculate the standard Pearson correlation coefficient matrix.
corr_matrix = df.corr()
print(corr_matrix)
assert corr_matrix.loc["HOURS", "SCORE"] > 0.99

Expected output

               HOURS  ATTENDANCE     SCORE
HOURS       1.000000    0.985955  0.994273
ATTENDANCE  0.985955    1.000000  0.993427
SCORE       0.994273    0.993427  1.000000

Alternative correlation methods (Spearman & Kendall) 🔝

Use the method parameter to compute rank-based non-parametric correlations such as "spearman" or "kendall".
spearman_corr = df.corr(method="spearman")
print(spearman_corr)
assert spearman_corr.loc["HOURS", "SCORE"] == 1.0

Expected output

            HOURS  ATTENDANCE  SCORE
HOURS         1.0         1.0    1.0
ATTENDANCE    1.0         1.0    1.0
SCORE         1.0         1.0    1.0

Exercise: compute advertising sales correlation 🔝

Given a DataFrame with AD_SPEND = [10, 20, 30, 40] and SALES = [15, 28, 42, 55], compute the correlation matrix and verify that the correlation between AD_SPEND and SALES exceeds 0.99.

Exercise solution 🔝

Solution constructing the DataFrame and applying corr().
ad_df = pd.DataFrame({"AD_SPEND": [10, 20, 30, 40], "SALES": [15, 28, 42, 55]})
answer = ad_df.corr()
print(answer)
assert answer.loc["AD_SPEND", "SALES"] > 0.99

Expected output

          AD_SPEND     SALES
AD_SPEND  1.000000  0.999298
SALES     0.999298  1.000000

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice corr() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas corr() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer