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.
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.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
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
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
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.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 corr() examples interactively directly in your browser:
Reference: Pandas corr() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.