To calculate a z-score, subtract the mean from your value and divide the result by the standard deviation: z = (value - mean) / standard deviation. The z-score tells you how many standard deviations a value is above the mean (positive) or below it (negative). A z-score of 0 means the value is exactly average.
How do you calculate a z-score step by step?
Take delivery times from the Bazaar online store. For the 787 completed orders, the mean delivery time is 2.99 days and the standard deviation is 1.15 days. One order took 6 days. Is that unusual?
1. Subtract the mean: 6 − 2.99 = 3.01 days above average.
2. Divide by the standard deviation: 3.01 ÷ 1.15 = 2.62.
The z-score is 2.62: this order was about 2.6 standard deviations slower than normal. Here is the same calculation in Python.
import pandas as pd orders = pd.read_csv("orders.csv") done = orders[orders["status"] == "completed"] days = done["delivery_days"] mean = days.mean() std = days.std(ddof=0) print(round(mean, 2), round(std, 2)) z = (6 - mean) / std print(round(z, 2)) print(round((1 - mean) / std, 2))
2.99 1.15 2.62 -1.74
The last line shows a fast order: 1 day gives a z-score of −1.74, which is below average, so the sign is negative. ddof=0 divides by the number of values, which is right when you treat the data as the whole group. For a sample, see the note in [sample vs population](/blog/sample-vs-population).
What does a z-score mean?
| z-score | Meaning |
|---|---|
| 0 | Exactly the mean |
| 1 | One standard deviation above the mean |
| −1 | One standard deviation below the mean |
| 2 or more | Far above the mean, uncommon |
| −2 or less | Far below the mean, uncommon |
The z-score has no unit. It is measured in standard deviations, so it works for days, dollars, centimeters or points. For data with a bell shape, about 68% of values have a z-score between −1 and 1, and about 95% between −2 and 2 (see [normal distribution and the 68-95-99.7 rule](/blog/normal-distribution)).
How do you calculate z-scores for a whole column?
Do the same calculation on every value at once. pandas applies it to the full column.
done = done.assign(z_days=(days - mean) / std) print(done["z_days"].round(2).head(3).tolist()) print(round(done["z_days"].mean(), 6), round(done["z_days"].std(ddof=0), 6)) print((done["z_days"].abs() > 2).sum(), (done["z_days"].abs() > 2).mean().round(3)) print(done.loc[done["z_days"] > 2, "delivery_days"].value_counts())
[0.0, 0.88, 0.88] 0.0 1.0 10 0.013 delivery_days 6.0 9 7.0 1 Name: count, dtype: int64
After the conversion, the new column always has a mean of 0 and a standard deviation of 1. Ten orders have a z-score beyond 2 in either direction: nine took 6 days and one took 7. That is 1.3% of the orders. SciPy has a shortcut: scipy.stats.zscore(days) gives the same values and also uses ddof=0 by default.
How do you use z-scores to compare different things?
Because z-scores have no unit, you can compare values from different groups. The Bazaar data has daily sales for two cafes. On one day, Corner Café made 130 sales and Maple Café made 120. Which day was stronger for its own cafe?
cafe = pd.read_csv("cafe.csv") stats_by_cafe = cafe.groupby("cafe")["sales"].agg(["mean", lambda s: s.std(ddof=0)]) stats_by_cafe.columns = ["mean", "std"] print(stats_by_cafe.round(1)) for name, sales in [("Corner Café", 130), ("Maple Café", 120)]: row = stats_by_cafe.loc[name] print(name, round((sales - row["mean"]) / row["std"], 2))
mean std cafe Corner Café 117.3 25.0 Maple Café 95.8 24.3 Corner Café 0.51 Maple Café 0.99
Corner Café made more sales, but 130 is only 0.51 standard deviations above its usual 117.3. Maple Café made fewer, but 120 is almost a full standard deviation above its usual 95.8. Relative to its own normal, Maple Café had the stronger day.
Can you turn a z-score into a percentage?
If the data is close to a bell shape, yes. The normal distribution gives the share of values below a z-score.
from scipy import stats print(round(stats.norm.cdf(z), 3)) print(round(stats.norm.cdf(1) - stats.norm.cdf(-1), 4), round(stats.norm.cdf(2) - stats.norm.cdf(-2), 4))
0.996 0.6827 0.9545
A z-score of 2.62 means a value above about 99.6% of the values, if the data were normal. In the real data, 99.9% of orders took 6 days or less, because delivery days are whole numbers and skewed. The percentage from a z-score is only as good as the bell-shape assumption.
Common mistakes
- Using z-scores on very skewed data. Order amounts in the same file have a maximum z-score of 8.03, because a few big orders stretch the standard deviation. For skewed data, the [IQR method](/blog/outliers-iqr-method) is often a better outlier check.
- Forgetting the sign. A negative z-score means below the mean, which is not an error.
- Mixing ddof settings. pandas
std()usesddof=1, and NumPy and SciPy useddof=0. Pick one and say which. - Calling every z-score above 2 an error. In a large data set, some values are expected to be that far out. Check them before removing them.
What to read next
Practice with the lesson [Distribution shape and z-score](/learn/statistics-data-analysis/distribution-shape-z-score). Review how the denominator is built in [standard deviation explained](/blog/standard-deviation-explained), or continue with the [Statistics and Data Analysis course](/courses/statistics-data-analysis) (44 lessons, free).
- #statistics
- #z-score
- #standard score
- #standard deviation