DS Standard Deviation
The standard deviation (SD, σ, or std) measures how far values typically lie from their mean. A small SD means the values cluster tightly around the average; a large SD means they are widely spread. It is expressed in the same unit as the data — rides, degrees, euros — which makes it easy to interpret.
Computing it step by step
The recipe:
- Compute the mean.
- Subtract the mean from every value (the deviations).
- Square the deviations, so negatives don't cancel positives.
- Average the squares — this is the variance.
- Take the square root to get back to the original unit.
import math
rides = [414, 298, 439, 405, 254, 523, 497, 428, 209, 368]
mean = sum(rides) / len(rides)
deviations = [x - mean for x in rides]
squares = [d ** 2 for d in deviations]
variance = sum(squares) / len(rides)
sd = math.sqrt(variance)
print("mean:", mean)
print("deviations:", [round(d, 1) for d in deviations])
print("variance:", round(variance, 2))
print("standard deviation:", round(sd, 2))mean: 383.5
deviations: [30.5, -85.5, 55.5, 21.5, -129.5, 139.5, 113.5, 44.5, -174.5, -15.5]
variance: 9356.65
standard deviation: 96.73
On these ten days, rentals typically differ from the average of 383.5 by about 97 rides.
Population or sample? (ddof)
When the data is a sample and you want to estimate the spread of the whole population, divide by n − 1 instead of n. This "Bessel's correction" compensates for the fact that a sample's values sit slightly closer to their own mean than to the true population mean.
The two libraries have different defaults — a classic source of confusion:
| Call | Divides by | Default in |
|---|---|---|
np.std(x) |
n (population) |
NumPy |
np.std(x, ddof=1) |
n − 1 (sample) |
— |
series.std() |
n − 1 (sample) |
pandas |
series.std(ddof=0) |
n (population) |
— |
import numpy as np
import pandas as pd
rides = [414, 298, 439, 405, 254, 523, 497, 428, 209, 368]
print("NumPy default (ddof=0):", np.std(rides).round(2))
print("NumPy ddof=1: ", np.std(rides, ddof=1).round(2))
print("pandas default (ddof=1):", round(pd.Series(rides).std(), 2))NumPy default (ddof=0): 96.73
NumPy ddof=1: 101.96
pandas default (ddof=1): 101.96
That's why the describe() table in the Functions lesson showed 102.0 while np.std gave 96.73. With 60 or more observations the difference becomes small; with 10 it is noticeable.
Comparing variability
Standard deviations become useful when you compare groups. Are rides more variable on weekdays or on weekends? And how much do the weather variables vary?
import numpy as np
import pandas as pd
rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
"humidity": humidity, "weekend": weekend, "rides": rides})
print(bikes[["temp_c", "rain_mm", "rides"]].std().round(2))
print()
print(bikes.groupby("weekend")["rides"].agg(["mean", "std"]).round(1))temp_c 7.37
rain_mm 3.97
rides 108.57
dtype: float64
mean std
weekend
0 327.4 100.5
1 432.5 93.5
Weekends have a higher mean, and their spread is similar to weekdays'. Note that you can't directly compare the SD of temp_c (°C) with that of rides (rides) — they're in different units.
The coefficient of variation
To compare spreads across different units or very different means, divide the SD by the mean. The result, the coefficient of variation (CV), is unit-free:
import numpy as np
stations = {
"Old Bridge": np.array([150, 112, 161, 147, 130, 189, 176]),
"Market Sq": np.array([139, 98, 151, 140, 61, 180, 170]),
"Riverside Park": np.array([125, 88, 127, 118, 63, 154, 151]),
}
for name, x in stations.items():
sd = x.std(ddof=1)
print(f"{name:<15} mean={x.mean():6.1f} sd={sd:5.1f} cv={sd / x.mean():.2f}")Old Bridge mean= 152.1 sd= 26.3 cv=0.17
Market Sq mean= 134.1 sd= 41.6 cv=0.31
Riverside Park mean= 118.0 sd= 32.8 cv=0.28
The 68–95 rule
For data that is roughly bell-shaped (normally distributed), about 68 % of values lie within one SD of the mean and about 95 % within two. This gives a quick feel: with a mean of 355 and an SD of 109 rides, a "normal" day has roughly 246–464 rides, and a day outside 138–573 would be unusual. You'll test this rule on real data in the Distributions lesson.
Note: The SD is sensitive to outliers, just like the mean. For skewed data the IQR from the Percentiles lesson is often a better description of spread.
Test yourself
What does a standard deviation of 0 mean?
- The mean is 0
- All values are identical
- The data is missing
- The data is normally distributed
pd.Series(x).std() and np.std(x) give different results. Why?
- pandas rounds the result
- pandas uses ddof=1 (sample), NumPy uses ddof=0 (population) by default
- NumPy ignores the first value
- One of them is wrong
In which unit is the standard deviation of daily rides expressed?
- Rides squared
- Rides
- Percent
- It has no unit