2026, Jan 05 15:00
Two simple ways to get the z-score for one observation in Python: scipy.stats.zscore indexing or the direct formula
Learn how to get the z-score for a single value in Python using scipy.stats.zscore or a direct mean/std formula (ddof=0). Examples, pitfalls, and indexing tips.
When you need the z-score for a single observation but call scipy.stats.zscore on the entire dataset, the function returns an array of standardized values, one per element. The question is simple: how do you get just the value for a specific X without rewriting the world? Let’s walk through a minimal dataset and the two straightforward ways to do it.
Problem setup
Suppose you have a small sample and want the z-score for one specific point. A hand-rolled computation might look like this:
samples = [25, 37, 15, 36, 92, 28, 33, 40]
probe = 40
mu = sum(samples) / len(samples)
sq_accum = 0
for j in range(len(samples)):
sq_accum += (samples[j] - mu) ** 2
sigma = ((1 / len(samples)) * sq_accum) ** 0.5
z_one = (probe - mu) / sigma
Trying the same via SciPy typically looks like this:
from scipy import stats as st
zs = st.zscore(samples)
The result is an array, one z-score per input element. For the example dataset above it evaluates to:
[-0.61219538 -0.05775428 -1.07422964 -0.10395771 2.48343411 -0.47358511 -0.24256798 0.08085599]
If your goal is just the z-score for one value X, returning an array can feel like overkill. The key is understanding what zscore returns and how to pick the one number you care about.
What’s actually happening
scipy.stats.zscore standardizes every element in the input and returns an array_like with the same shape as the input. There’s no special treatment for a single lookup. If you want only the z-score for one element of the original list, you either compute it directly from mean and standard deviation or take the returned array and index the corresponding position. If that specific value isn’t in the list and you try to find it by value, Python will raise a ValueError.
Two reliable ways to get the z-score for one X
The first way is to compute the z-score directly using the population mean and standard deviation. The second way is to compute all z-scores and select the one at the index of the value you care about.
Direct formula, consistent with the dataset and ddof=0:
import numpy as np as npy
vals = [25, 37, 15, 36, 92, 28, 33, 40]
point = 40
mu_val = npy.mean(vals)
sigma_val = npy.std(vals, ddof=0)
z_val = (point - mu_val) / sigma_val
print(z_val) # 0.08085599417810461
Using scipy.stats.zscore and then selecting the matching entry by index:
from scipy import stats as st
vals = [25, 37, 15, 36, 92, 28, 33, 40]
point = 40
zs_all = st.zscore(vals, ddof=0)
pos = vals.index(point) # raises ValueError if point not in vals
z_val = zs_all[pos]
print(z_val) # 0.08085599417810461
Both approaches produce the same value for the example above.
Why this matters
Working with standardized data is a common step in analytics and feature engineering. Knowing that scipy.stats.zscore returns an array helps you avoid misinterpreting results and lets you decide between a full standardization pass or a single-point computation. It also makes error handling explicit: if you plan to locate the point via list.index, be aware that a missing value will raise a ValueError.
Takeaways
If you only need the z-score for one value, compute it directly from mean and standard deviation or compute the full z-score array and pick the matching index. Be explicit about ddof=0 to keep the calculation consistent across methods. And if you rely on value-based lookup, handle the case where the value might not be present.