Correlation measures how closely two numeric variables move together. The most common measure is the Pearson correlation coefficient r, which runs from -1 to +1. Near +1 means they rise together, near -1 means one rises as the other falls, and near 0 means no straight-line pattern. Correlation does not show that one variable causes the other.
How do you check correlation in Python?
Bazaar, a fictional online store, tracks daily ad spend and website visitors. Here are the first 28 days of 2026 (ad spend in dollars, visitors, weekend flag, orders):
import io import pandas as pd from scipy import stats raw = "266,1538,0,1;147.5,1376,0,5;320.5,1747,1,8;253,1725,1,5;215.5,1509,0,7;256.5,1591,0,9;165.5,1472,0,4;245.5,1473,0,6;241,1547,0,2;323,1927,1,6;236,1775,1,3;193,1438,0,6;190,1442,0,5;143,1358,0,2;205,1378,0,3;208,1416,0,6;292.5,1868,1,9;254,1792,1,3;206,1481,0,7;211,1375,0,4;250.5,1415,0,3;202,1407,0,10;190,1380,0,3;299.5,1875,1,4;246,1622,1,10;212,1409,0,4;147,1359,0,3;209.5,1449,0,7" df = pd.read_csv(io.StringIO("ad_spend,visitors,weekend,orders\n" + raw.replace(";", "\n"))) print(len(df)) print(df[["ad_spend", "visitors", "orders"]].corr().round(2)) r, p = stats.pearsonr(df["ad_spend"], df["visitors"]) print(round(r, 3), p < 0.001)
28
ad_spend visitors orders
ad_spend 1.00 0.83 0.25
visitors 0.83 1.00 0.20
orders 0.25 0.20 1.00
0.827 Truecorr() gives a table of correlations for all numeric columns. The diagonal is 1 because each column correlates perfectly with itself. Ad spend and visitors have r = 0.83, a strong positive link. Ad spend and orders have r = 0.25, a weak one.
How do you read r?
| r value | Rough meaning |
|---|---|
| 0.7 to 1.0 | Strong positive |
| 0.3 to 0.7 | Moderate positive |
| -0.3 to 0.3 | Weak or none |
| -0.7 to -0.3 | Moderate negative |
| -1.0 to -0.7 | Strong negative |
These cutoffs are habits, not rules, and they vary by field. In a survey about human behavior, 0.4 can be notable. In a physics experiment it would be weak.
Does correlation imply causation?
No. Ad spend and visitors move together, but that alone does not show that spending causes visits. Three other explanations can create the same pattern:
- A common cause. Look at the weekend column. Weekends are a third factor that lifts both numbers.
- Reverse direction. Busy periods might push the team to spend more on ads.
- Chance. With only 28 days, a strong pattern can appear by luck.
Let's test the first one:
print(df.groupby("weekend")[["ad_spend", "visitors"]].mean().round(0)) weekdays = df[df["weekend"] == 0] print(len(weekdays), round(weekdays["ad_spend"].corr(weekdays["visitors"]), 2))
ad_spend visitors weekend 0 205.0 1441.0 1 278.0 1791.0 20 0.69
On weekend days Bazaar spends more ($278 vs $205 on average) and also gets more visitors (1,791 vs 1,441). So weekends explain part of the link. But among the 20 weekdays alone, r is still 0.69. The data cannot tell you how much of the rest is real effect, reverse direction or chance.
A classic example from outside the store: ice cream sales and drowning cases rise together in summer. Ice cream does not cause drowning. Hot weather drives both.
What is a spurious correlation?
A spurious correlation is a strong link between two variables with no direct cause between them. It comes from a hidden third variable, from chance in small data, or from picking pairs out of hundreds of options. The more pairs you test, the more likely some look related by luck.
How do you show causation?
The strongest tool is an experiment. Bazaar could raise ad spend on randomly chosen days and keep it flat on others, then compare visitors. Random choice spreads weekends and other factors evenly across both groups, so a remaining difference can be linked to the ads. In online products, this is an [A/B test](/blog/ab-testing-explained).
What can correlation miss?
- Curved relationships. A pattern that rises and then falls can give r near 0. Always draw a scatterplot first.
- Outliers. One extreme point can change r a lot.
- The size of the effect. r says how regular the link is, not how many extra visitors each extra dollar brings. That is the slope of a regression line:
fit = stats.linregress(df["ad_spend"], df["visitors"]) print(round(fit.slope, 2), round(fit.rvalue ** 2, 2))
3.04 0.68
On these days, each extra $1 of ad spend came with about 3 more visitors. This describes a pattern, not a promise: "if I add $100, I will get 300 visitors" needs an experiment. The second number is r², which is 0.68: about 68% of the ups and downs in visitors line up with ad spend in a straight line. r itself is not read as a percentage.
Common mistakes
- Reading cause from correlation. "Move together" and "one drives the other" are different claims.
- Skipping the plot. Very different data sets can share the same r.
- Reading r as a percent. r = 0.5 does not mean "50% related".
- Drawing big conclusions from few points. 28 days raise a good question, not a final answer.
What to read next
The lessons [Scatterplots and correlation](/learn/statistics-data-analysis/scatterplot-correlation) and [Correlation and causation](/learn/statistics-data-analysis/correlation-causation) cover this with the full Bazaar data. For hands-on practice, try the project [Correlation or causation?](/projects/correlation-or-causation). You can also see the full [Statistics and Data Analysis course](/courses/statistics-data-analysis).
- #statistics
- #correlation
- #causation
- #pearson