Exploring Correlation in Python (original) (raw)

Last Updated : 6 Feb, 2026

Correlation is one of the most commonly used statistical measures to understand how variables are related to each other. In Python, correlation helps identify whether two variables move together, move in opposite directions or have no relationship at all.

pearson_correlation_coefficient

Correlation

Correlation measures the strength and direction of the relationship between two numerical variables. Value ranges from -1 to +1

Common Correlation Methods in Python

1. Pearson Correlation

Pearson Correlation measures linear relationship between two continuous variables.

2. Spearman Correlation

Spearman Correlation measures monotonic relationship using ranks.

3. Kendall Correlation

Kendall Correlation measures rank consistency between variables.

Correlation Using Python

Python provides built-in tools through pandas and visualization libraries to compute and analyze correlation efficiently. Understanding correlation helps build better models and gain deeper insights from data.

1. Sample Dataset

Here we will create a sample dataset and use it using pandas dataframe. We will use seaborn and matplotlib to visualize the relationship.

Python `

import pandas as pd import seaborn as sns import matplotlib.pyplot as plt

data = { 'Math': [78, 85, 96, 80, 86], 'Science': [88, 90, 94, 82, 89], 'English': [72, 75, 78, 70, 74] }

df = pd.DataFrame(data) df

`

**Output:

Screenshot-2026-02-05-120733

Data

2. Pearson Correlation

pearson_corr = df.corr(method='pearson') print(pearson_corr)

sns.heatmap(pearson_corr, annot=True, cmap='coolwarm') plt.title("Pearson Correlation Heatmap") plt.show()

`

**Output:

Screenshot-2026-02-05-120855

Pearson Correlation

The above output shows that the relationship between maths, science and english.

3. Spearman Correlation

spearman_corr = df.corr(method='spearman') print(spearman_corr)

sns.heatmap(spearman_corr, annot=True, cmap='viridis') plt.title("Spearman Correlation Heatmap") plt.show()

`

**Output:

Screenshot-2026-02-05-123518

Spearman Correlation

4. Kendall Correlation

kendall_corr = df.corr(method='kendall') print(kendall_corr)

sns.heatmap(kendall_corr, annot=True, cmap='plasma') plt.title("Kendall Correlation Heatmap") plt.show()

`

**Output:

Screenshot-2026-02-05-123734

Kendall Correlation

5. Correlation Between Two Columns

corr_value = df['Math'].corr(df['Science']) print("Correlation between Math and Science:", corr_value)

two_col_corr = df[['Math', 'Science']].corr()

sns.heatmap(two_col_corr, annot=True, cmap='coolwarm') plt.title("Correlation Between Math and Science") plt.show()

`

**Output:

Screenshot-2026-02-05-124740

Correlation Between Two Columns

Interpreting Correlation Values

Correlation Value Meaning
0.8 to 1.0 Strong positive
0.5 to 0.8 Moderate positive
0.0 to 0.5 Weak positive
0 No correlation
-0.5 to 0 Weak negative
-0.8 to -0.5 Moderate negative
-1.0 to -0.8 Strong negative

Limitations of Correlation

Applications of Correlation