Correlation Analysis is a cornerstone of data science. It uncovers hidden relationships between variables in your data, revealing the strength and direction of these connections and helping you make informed decisions and accurate predictions.
Data science relies heavily on correlations. Correlation Analysis is essential for understanding relationships between data points, and the two most common types - Spearman and Pearson - are widely used in data science.
Pearson correlation is used when both variables are continuous, their relationship looks like a straight line, and there are no extreme outliers. Spearman correlation, on the other hand, is used when the data is ranked or ordinal (ratings, positions), when the relationship rises or falls consistently but not in a straight line, or when outliers distort the picture.
Pearson's r measures linear relationships from the raw values; Spearman's ρ (rho) measures monotonic relationships from ranks. Both range from −1 to +1.
This article explains both coefficients, their formulas, a solved example, and the Python code to calculate them, so you can pick the right one for your data.
Pearson vs Spearman Correlation: Key Differences
Basis | Pearson correlation (r) | Spearman correlation (ρ) |
What it measures | Strength and direction of a linear relationship | Strength and direction of a monotonic relationship |
Works on | Raw values | Ranks of the values |
Data type | Continuous (interval or ratio) | Ordinal, or continuous data converted to ranks |
Type of method | Parametric | Non-parametric |
Effect of outliers | Highly sensitive | Much less sensitive |
Key assumptions | Linear relationship; roughly normal data for the significance test | Monotonic relationship; no distribution assumption |
Range | −1 to +1 | −1 to +1 |
Also called | Karl Pearson's coefficient of correlation; product-moment correlation | Spearman's rank correlation coefficient (rₛ) |
Typical use | Height vs weight, ad spend vs website leads | Survey ratings, exam ranks, skewed data such as income |
The rest of this article explains each row, starting with what correlation measures.
What is Correlation?
Correlation measures how strongly two variables move together and in which direction. A positive correlation means both rise together, a negative correlation means one rises as the other falls, and zero means there is no consistent pattern. In summer, for example, household electricity use in Indian cities tends to rise with temperature as fans and ACs run longer: a positive correlation.
Two shapes of relationship decide which method you should use:
Linear: one variable changes by a roughly constant amount for each unit change in the other, so the points sit along a straight line.
Monotonic: the variables keep moving in the same direction (always up, or always down), but the rate of change can vary. Every linear relationship is monotonic, but not every monotonic relationship is linear.
Pearson captures the first shape, and Spearman captures the second. Neither proves that one variable causes the other; a correlation only shows that they move together.
What is Pearson's Correlation (r)?
Pearson's correlation coefficient (r) measures how closely two continuous variables follow a straight line. English mathematician Karl Pearson developed it, which is why Indian textbooks often call it Karl Pearson's coefficient of correlation.
It is calculated as the covariance of the two variables divided by the product of their standard deviations. That division makes r unit-free, so you can correlate height in centimeters with weight in kilograms.
Pearson Correlation Coefficient Formula

Here x̄ and ȳ are the means of x and y, and each sum runs over all n pairs of observations.
You can use the Pearson coefficient only if both variables are quantitative, normally distributed, have no outliers, and the relationship is linear.
When is Pearson's r Valid?
Pearson's r gives a reliable answer only when your data meets these conditions:
Both variables are continuous, such as salary, temperature, or marks.
The relationship is roughly linear - Draw a scatter plot first; a curved pattern will lower r even when the link is strong.
There are no extreme outliers - A single extreme point can pull r up or down sharply.
The data is roughly normal, if you need a p-value - You can calculate r without normality, but the standard significance test assumes it, especially for small samples.
How to Interpret Pearson's r
The sign shows the direction, and the size shows the strength. A common rule of thumb treats values below 0.3 (in either direction) as weak, 0.3 to 0.7 as moderate, and above 0.7 as strong. These cut-offs vary by field: for instance, in medical or social research, an r of 0.3 can be important.


Squaring r tells you how much of the variation in one variable is explained by a straight-line relationship with the other. An r of 0.8, for example, gives r² = 0.64, or 64%.
One caution: r = 0 means there is no linear relationship, not that there is no relationship at all. The U-shaped example later in this guide shows a perfect relationship that Pearson scores as zero.
What is Spearman Correlation (ρ)?
British psychologist Charles Spearman introduced Spearman's rank correlation coefficient (ρ, also written rₛ). It measures how well the relationship between two variables follows a consistent upward or downward trend.
Instead of the raw values, Spearman works on ranks. You rank each variable from highest to lowest, then measure how closely the two sets of ranks agree. In fact, Spearman's ρ is simply Pearson's r calculated on the ranks.
Working with ranks gives Spearman two practical strengths:
It suits ordinal data, such as 1–5 customer ratings, Likert-scale survey answers or class positions, where the gaps between values are not equal.
It barely reacts to outliers. The largest value simply takes rank 1, no matter how extreme it is.
Like Pearson's r, ρ ranges from −1 to +1. A value of +1 means that whenever one variable increases, the other always increases too, even if not by a constant amount.


Spearman Rank Correlation Formula
When no two values share a rank, Spearman's ρ is:

Here dᵢ is the difference between the two ranks of each observation, and n is the number of pairs.
What Happens If Two Values Tie?
Tied values each get the average of the ranks they occupy. If two students share the 2nd and 3rd highest marks, both get rank 2.5.
With ties, the shortcut formula above is only approximate. Software such as SciPy and pandas avoids the problem by calculating Pearson's r on the ranks. Indian Class 12 and B.Com syllabi instead add a correction factor of (m³ − m)/12 for each group of m tied values:

When to Use Pearson vs Spearman?
With both coefficients defined, the choice comes down to your data type, the shape of the relationship, and outliers.
Use Pearson correlation when:
Both variables are continuous, such as advertising spend and website leads;
A scatter plot shows the points clustering around a straight line;
There are no extreme outliers;
You plan to build a linear regression model, which assumes the same straight-line relationship.
Use Spearman correlation when:
One or both variables are ordinal, such as app ratings, survey scales, or rankings;
The relationship keeps rising or falling but curves, as salary often does with years of experience;
The data has outliers or is heavily skewed, as income and property prices usually are;
The sample is too small to check whether the data is roughly normal.
If you are unsure, plot the data and calculate both. When the two values are close, the relationship is close to linear. When Spearman is clearly higher, the relationship is monotonic but curved, or an outlier is pulling Pearson down.
Three Mistakes are Common with Either Method:
Using them on categories: Gender or city is not a ranked variable. Use a chi-square test for two categorical variables, or point-biserial correlation for a yes/no variable against a numeric one.
Reading correlation as causation: Ice-cream sales and AC sales both rise in May, but neither causes the other; the heat drives both.
Skipping the scatter plot: A single number can hide a curve, a cluster, or one extreme point.
Comparative Analysis of Pearson vs Spearman Correlation
The clearest way to see the difference is to run both methods on the same data. The chart and table below cover four patterns. The figures are illustrative, and I calculated each pair of values in Python.

Pattern | Example | Pearson r | Spearman ρ |
Straight-line rise | Weeks of typing practice vs typing speed | 1.00 | 1.00 |
Curved but always rising | Years of experience vs salary (₹ LPA) | 0.94 | 1.00 |
Rising, with one outlier | Hours studied per week vs mock test score | 0.83 | 1.00 |
U-shaped | y = x² for x from −5 to 5 | 0.00 | 0.00 |
Straight-line rise: Typing speed grows by roughly the same amount each week, so the points fall on a line. Both methods return 1.00, because a perfect linear relationship is also perfectly monotonic.
Curved but always rising: Salary climbs quickly in the early years and then flattens. Every extra year still means a higher salary, so Spearman stays at 1.00, while Pearson drops to 0.94 because the points bend away from a straight line.
Rising, with one outlier: Five students studied 2–9 hours a week; one studied 20 hours but scored only a little more than the next student. That single point drags Pearson down to 0.83. Spearman still returns 1.00, because the student with the most hours also has the highest score.
U-shaped: Here y depends entirely on x, yet both coefficients are zero. The relationship falls and then rises, so it is neither linear nor monotonic. This is why you should always plot the data before trusting either number.
A Solved Example: Spearman's Rank Correlation Step by Step
Suppose six students take a Maths test and a Statistics test, and you want to know whether students who do well in one also do well in the other.
Step 1: Rank each subject. Give rank 1 to the highest mark in each column.
Step 2: Find the rank difference (d) for each student and square it.
Student | Maths marks | Statistics marks | Maths rank | Statistics rank | d | d² |
A | 85 | 78 | 2 | 3 | −1 | 1 |
B | 62 | 70 | 5 | 4 | 1 | 1 |
C | 74 | 65 | 3 | 5 | −2 | 4 |
D | 90 | 88 | 1 | 1 | 0 | 0 |
E | 55 | 58 | 6 | 6 | 0 | 0 |
F | 68 | 80 | 4 | 2 | 2 | 4 |
Total | 10 |
Step 3: Apply the formula

with n = 6 and Σd² = 10.
Step 4: Interpret the result. A ρ of 0.71 shows a strong positive relationship: students who rank higher in Maths tend to rank higher in Statistics too. Pearson's r for the same marks is 0.78; the two values are close because the marks are spread fairly evenly, with no extreme scores.
How to Calculate Pearson and Spearman Correlation in Python
In practice, you will rarely rank data by hand. SciPy calculates each coefficient with its p-value, and pandas builds a full correlation matrix in one line. This code uses the same six students:
import pandas as pd
from scipy import stats
marks = pd.DataFrame({
"maths": [85, 62, 74, 90, 55, 68],
"statistics": [78, 70, 65, 88, 58, 80],
})
r, p_r = stats.pearsonr(marks["maths"], marks["statistics"])
rho, p_rho = stats.spearmanr(marks["maths"], marks["statistics"])
print(f"Pearson r = {r:.3f} (p = {p_r:.3f})")
print(f"Spearman rho = {rho:.3f} (p = {p_rho:.3f})")
# Correlation matrix for every numeric column
print(marks.corr(method="pearson"))
print(marks.corr(method="spearman"))Output:
Pearson r = 0.785 (p = 0.064)
Spearman rho = 0.714 (p = 0.111)The Spearman value matches the hand calculation. On a real dataset with many columns, df.corr(method="spearman") is the quickest way to scan for related variables during exploratory data analysis in Python.
Is the Correlation Statistically Significant?
The p-value tests whether the correlation could have arisen by chance when the true correlation is zero. Here both p-values are above 0.05, so six students are too few to rule out chance, even though the coefficients are fairly high. A larger sample would settle it.
For small samples, the SciPy documentation notes that the Spearman p-value is accurate only for large datasets and suggests a permutation test instead. Our article on descriptive vs inferential statistics explains how significance testing fits into the wider analysis.
What About Kendall's Tau?
Kendall's tau (τ) is a third option and, like Spearman, works on ranks. It compares every pair of observations and counts how many pairs are ordered the same way in both variables. It is often preferred for small samples or data with many tied ranks.
Kendall's τ is usually smaller than Spearman's ρ for the same data; for the six students it is 0.60. So compare τ values only with other τ values. In Python, use stats.kendalltau() or df.corr(method="kendall").
All three measures sit within the wider family of parametric and non-parametric tests, where Pearson is the parametric option and Spearman and Kendall are the non-parametric ones.
Conclusion
Pearson's r tells you how well two continuous variables fit a straight line. Spearman's ρ tells you how consistently they move in the same direction, using ranks. Check the scatter plot, the data type, and any outliers before you choose, or calculate both and read the gap between them.
Correlation is usually the first step towards models such as linear regression. If you want to practice these methods on real business datasets, the AnalytixLabs Certification Course in Data Analytics and Data Science both cover applied statistics with Python.
FAQs
What is the difference between Pearson and Spearman correlation?
Pearson correlation measures how closely two continuous variables follow a straight line, using their raw values. Spearman correlation measures whether they move consistently in the same direction, using ranks. Spearman therefore handles ordinal data, curved monotonic relationships, and outliers better, while Pearson is the standard choice for linear relationships between continuous variables.
When should I use Spearman instead of Pearson?
Use Spearman when your data is ordinal (ratings, ranks, survey scales), when the relationship curves but keeps rising or falling, or when outliers or heavy skew would distort Pearson. If the scatter plot shows a clean straight-line pattern between two continuous variables, Pearson is the better choice.
Is Pearson's r the same as Spearman's rho?
No. Spearman's rho is Pearson's r calculated on the ranks of the data rather than the raw values. The two match when the data is already in rank form, and both equal ±1 for a perfectly linear relationship. Otherwise they usually differ, most clearly when the relationship curves or contains outliers.
What does "Spearman's p" mean?
It is usually Spearman's ρ, the Greek letter rho, typed as a "p" because it looks similar. Rho is the correlation coefficient itself. It is different from the p-value, which tells you whether the correlation is statistically significant.
Which is better, Pearson or Spearman correlation?
Neither is better in general; each answers a different question. Pearson is more precise when its conditions hold: continuous variables, a linear relationship, and no extreme outliers. Spearman is safer when those conditions fail, because it needs only a monotonic relationship and is far less affected by extreme values.