Average
In everyday language an average is a "typical" value. In statistics there are several different averages, called measures of central tendency, and they can give quite different answers for the same data. The three classic ones are:
| Measure | Definition | Works for |
|---|---|---|
| Mean | sum of the values divided by how many there are | interval and ratio data |
| Median | the middle value when the data is sorted | ordinal, interval and ratio data |
| Mode | the most frequent value | every type of data, including nominal |
The next three lessons explore each one. This lesson compares them, because choosing the right average is often more important than computing it.
Three averages, one dataset
Ten people at a small start-up earn these annual salaries (in thousands):
import statistics
salaries = [38, 42, 42, 45, 47, 50, 52, 55, 60, 240] # the last one is the founder
print("mean :", statistics.mean(salaries))
print("median:", statistics.median(salaries))
print("mode :", statistics.mode(salaries))mean : 67.1
median: 48.5
mode : 42
The founder's salary drags the mean far above what nine of the ten people earn. A job advert saying "average salary 67k" would be technically true and thoroughly misleading. The median, 48.5k, describes a typical employee much better.
Robustness
A statistic is robust (or resistant) if a few extreme values cannot move it much. The median is robust; the mean is not. Watch what happens when we make the founder's salary even larger:
import statistics
base = [38, 42, 42, 45, 47, 50, 52, 55, 60]
for founder in [65, 240, 1000, 5000]:
data = base + [founder]
print(f"founder={founder:5d} mean={statistics.mean(data):7.1f} median={statistics.median(data)}")founder= 65 mean= 49.6 median=48.5
founder= 240 mean= 67.1 median=48.5
founder= 1000 mean= 143.1 median=48.5
founder= 5000 mean= 543.1 median=48.5
The median never moves, because the founder stays "the largest value" no matter how large it gets. The mean follows the founder all the way up.
Which average should you use?
| Situation | Best choice | Why |
|---|---|---|
| Symmetric numeric data without outliers | mean | uses all the information; well-understood mathematically |
| Skewed data or outliers (income, house prices, waiting times) | median | robust, describes the typical case |
| Categorical data (favourite colour, product sold) | mode | the only one that makes sense |
| Ordinal data (ratings, sizes) | median (or mode) | needs order but not distances |
Skewness and the order of mean and median
For skewed data the mean is pulled towards the long tail, so the order of the averages reveals the shape:
- right-skewed:
mode < median < mean - symmetric:
mode ≈ median ≈ mean - left-skewed:
mean < median < mode
import numpy as np
import pandas as pd
rng = np.random.default_rng(9)
waits = pd.Series(rng.exponential(scale=6, size=5000).round()) # waiting times in minutes
print("mode :", waits.mode()[0])
print("median:", waits.median())
print("mean :", round(waits.mean(), 2))mode : 1.0
median: 4.0
mean : 6.06
Waiting times are right-skewed: many short waits, a few very long ones. The averages appear in exactly the predicted order.
All the averages in Python
| Task | statistics module |
NumPy | pandas |
|---|---|---|---|
| Mean | statistics.mean(x) |
np.mean(x) |
s.mean() |
| Median | statistics.median(x) |
np.median(x) |
s.median() |
| Mode | statistics.mode(x) / multimode(x) |
(via np.unique) |
s.mode() |
import numpy as np
import pandas as pd
ratings = [4, 5, 3, 4, 4, 2, 5, 4, 3, 5]
s = pd.Series(ratings)
print("numpy mean/median:", np.mean(ratings), np.median(ratings))
print("pandas mean/median:", s.mean(), s.median())
print("pandas mode:", s.mode().tolist())numpy mean/median: 3.9 4.0
pandas mean/median: 3.9 4.0
pandas mode: [4]
Tip: When you report an average, say which one it is. "The median house price" and "the mean house price" can differ by tens of thousands.
Test yourself
Which average is *not* affected when the largest value in a dataset is doubled?
- Mean
- Median
- Both
- Neither
Which average would you report for the most popular ice-cream flavour?
- Mean
- Median
- Mode
- Standard deviation
For a dataset, mean = 82, median = 75. Which shape is most likely?
- Left-skewed
- Symmetric
- Right-skewed
- Impossible to say anything