Mean
The arithmetic mean is what most people call "the average": add all the values and divide by how many there are.
sample mean: x̄ = (x₁ + x₂ + … + xₙ) / n = Σx / n
population mean: μ = Σx / NΣ (capital sigma) means "sum of". The formula is identical for samples and populations. Only the symbol changes, to remind you which one you are describing.
Computing the mean
A runner logged her weekly distances (km) over 8 weeks:
import statistics
km = [21.5, 18.0, 25.2, 30.1, 19.8, 27.4, 24.0, 22.6]
total = sum(km)
n = len(km)
print("sum =", round(total, 1))
print("n =", n)
print("mean =", round(total / n, 3))
print("check =", round(statistics.mean(km), 3))sum = 188.6
n = 8
mean = 23.575
check = 23.575
The balance point
The mean is the balance point of the data: the deviations above the mean exactly cancel those below it, so the sum of x − x̄ is always zero:
km = [21.5, 18.0, 25.2, 30.1, 19.8, 27.4, 24.0, 22.6]
mean = sum(km) / len(km)
deviations = [round(x - mean, 3) for x in km]
print("deviations:", deviations)
print("sum of deviations:", round(sum(deviations), 10))deviations: [-2.075, -5.575, 1.625, 6.525, -3.775, 3.825, 0.425, -0.975]
sum of deviations: 0.0
If you put the values as weights on a see-saw, the see-saw balances at the mean. This also explains why one far-away value has so much influence: it sits at the end of a long lever.
Weighted mean
Sometimes values do not count equally. A weighted mean multiplies each value by a weight w before summing:
weighted mean = Σ(w × x) / ΣwA course grade made of homework (20%), a midterm (30%) and a final exam (50%):
import numpy as np
scores = np.array([92, 74, 81]) # homework, midterm, final
weights = np.array([0.2, 0.3, 0.5])
print("plain mean :", round(scores.mean(), 2))
print("weighted mean:", np.average(scores, weights=weights))plain mean : 82.33
weighted mean: 81.1
A very common use is computing the mean from a frequency table: each value is weighted by how often it occurs. A survey of 25 households found how many cars each owns:
import numpy as np
cars = np.array([0, 1, 2, 3])
households = np.array([4, 11, 8, 2])
mean_cars = np.average(cars, weights=households)
print("households:", households.sum())
print("cars in total:", (cars * households).sum())
print("mean cars per household:", mean_cars)households: 25
cars in total: 33
mean cars per household: 1.32
Other means
| Mean | Formula | Typical use |
|---|---|---|
| Arithmetic | Σx / n |
most quantities |
| Geometric | (x₁ × x₂ × … × xₙ)^(1/n) |
growth rates, ratios |
| Harmonic | n / Σ(1/x) |
rates such as speeds over equal distances |
If an investment grows by +50% one year and −50% the next, the arithmetic mean of the growth factors (1.5 and 0.5) is 1.0, suggesting no change, but you actually end with 75% of your money. The geometric mean gets it right:
import statistics
factors = [1.5, 0.5]
print("arithmetic mean factor:", statistics.mean(factors))
print("geometric mean factor :", round(statistics.geometric_mean(factors), 4))
print("money after 2 years :", 100 * 1.5 * 0.5)
speeds = [60, 40] # km/h on two equal-length stretches
print("average speed (harmonic):", statistics.harmonic_mean(speeds), "km/h")arithmetic mean factor: 1.0
geometric mean factor : 0.866
money after 2 years : 75.0
average speed (harmonic): 48.0 km/h
The geometric mean factor of about 0.866 means "about −13.4% per year", which correctly turns 100 into 75 after two years. Driving equal distances at 60 and 40 km/h averages 48 km/h, not 50, because you spend more time at the slower speed.
The mean in pandas
import pandas as pd
df = pd.DataFrame({
"store": ["A", "A", "B", "B", "B", "C"],
"sales": [120, 135, 90, None, 110, 150],
})
print("overall mean:", round(df["sales"].mean(), 2)) # NaN is skipped
print(df.groupby("store")["sales"].mean())overall mean: 121.0
store
A 127.5
B 100.0
C 150.0
Name: sales, dtype: float64
pandas skips missing values (NaN) by default, so the overall mean is over 5 values, not 6.
Test yourself
What is the mean of 4, 8, 6 and 2?
- 4
- 5
- 6
- 20
What is the sum of the deviations x − x̄ for any dataset?
- Always positive
- Always zero
- Equal to the variance
- Equal to n
A class has 10 students averaging 70 points and another has 30 students averaging 90. What is the mean over all 40 students?
- 80
- 85
- 75
- 87.5