In statistics, a population is the whole group you want to learn about, and a sample is the smaller part of that group that you actually measure. You use the sample to estimate a number for the population, such as its average. A sample number is called a statistic, and the matching population number is called a parameter.
What is the difference between a sample and a population?
| Population | Sample | |
|---|---|---|
| What it is | Everyone or everything you care about | The part you measured |
| Example | All customers of an online store | 30 customers chosen at random |
| Number called | Parameter | Statistic |
| Usual symbols | mean μ, standard deviation σ | mean x̄, standard deviation s |
| Known? | Usually not | Yes, you have the data |
The population depends on your question. If you ask "How old are the customers of the Bazaar online store?", the population is all Bazaar customers. If you ask "How old are online shoppers in general?", the population is much bigger, and the Bazaar customers are only a sample of it.
Can you see the difference with real data?
The Bazaar store has a table of 577 customers. Some ages are missing, so we keep the 522 customers with an age. For this example, treat those 522 as the whole population. Then we know the true average, and we can check how well a sample guesses it.
import pandas as pd customers = pd.read_csv("customers.csv") ages = customers["age"].dropna() print(len(customers), len(ages)) print(round(ages.mean(), 2), round(ages.std(ddof=0), 2))
577 522 33.34 9.0
The population mean (μ) is 33.34 years and the population standard deviation (σ) is 9.0. Now take three random samples of 30 customers each.
for seed in [1, 2, 3]: sample = ages.sample(n=30, random_state=seed) print(seed, len(sample), round(sample.mean(), 2), round(sample.mean() - ages.mean(), 2))
1 30 33.83 0.5 2 30 31.73 -1.6 3 30 34.63 1.3
Each sample mean is close to 33.34 but not equal to it. The last column is the sampling error: the gap between the sample statistic and the population parameter. Each new sample gives a different number. This is called sampling variability.
How big should a sample be?
A larger sample gives a more stable estimate. Here we draw 1,000 samples for each size and look at how far the sample means spread.
for n in [10, 30, 100, 300]: means = pd.Series([ages.sample(n=n, random_state=i).mean() for i in range(1000)]) print(n, round(means.min(), 1), round(means.max(), 1), round(means.std(), 2))
Output (columns: sample size, smallest mean, largest mean, standard deviation of the means):
10 24.4 42.2 2.77 30 28.7 38.2 1.54 100 30.2 36.1 0.83 300 32.2 34.5 0.35
With 10 customers, a sample mean can land anywhere from 24.4 to 42.2. With 300, it stays between 32.2 and 34.5. The spread shrinks as the sample grows, but slowly: going from 30 to 300 (10 times more data) cuts the spread by about 4 times, not 10.
Size is only half the story. A sample must also be chosen fairly. A sample of 300 customers who all signed up through one channel can mislead you more than a random sample of 30.
Why does the sample standard deviation divide by n - 1?
A sample tends to look a little less spread out than its population, because the extreme values are less likely to be in it. If you divide by n, the sample variance is too small on average. Dividing by n - 1 corrects this.
pandas does this by default (ddof=1). NumPy uses ddof=0 by default. Here is a check with 2,000 small samples of 5 customers:
import numpy as np print(round(ages.var(ddof=0), 2)) by_n = np.mean([ages.sample(n=5, random_state=i).var(ddof=0) for i in range(2000)]) by_n_minus_1 = np.mean([ages.sample(n=5, random_state=i).var(ddof=1) for i in range(2000)]) print(round(by_n, 2), round(by_n_minus_1, 2))
80.97 64.12 80.15
The true population variance is 80.97. Dividing by n gives an average of 64.12, which is too low. Dividing by n - 1 gives 80.15, much closer. Use ddof=0 only when your data is the whole population.
Common mistakes
- Treating a sample as the population. A survey of 30 friends tells you about those friends, not about all students.
- Choosing the sample by convenience. People who answer a survey are often different from people who do not.
- Thinking a bigger sample fixes a biased one. More biased data gives a more precise wrong answer.
- Forgetting that the answer changes each time. Report how uncertain the estimate is, for example with a [confidence interval](/blog/confidence-interval-explained).
What to read next
The lesson [Sampling variability](/learn/statistics-data-analysis/sampling-variability) lets you run these experiments yourself on the Bazaar data. Next, see how spread is measured in [standard deviation explained](/blog/standard-deviation-explained), or visit the [Statistics and Data Analysis course](/courses/statistics-data-analysis) (44 lessons, free).
- #statistics
- #sample
- #population
- #sampling