Sampling Methods
Random, stratified, cluster.
How you choose a sample matters as much as how big it is. There are a few standard ways to sample, and each one fits a different situation.

- How you choose matters
- Not just how many
- A few standard ways
Four ways to sample
A simple random sample gives everyone the same chance. A stratified sample splits the population into groups first, like regions, and samples from each one, so no group is missed. A cluster sample picks whole groups, like a few schools. And a systematic sample takes every tenth person on a list.
| Method | How | Good for |
|---|---|---|
| Simple random | everyone equal chance | the default |
| Stratified | sample within each group | small groups matter |
| Cluster | pick whole groups | cheap, spread out |
| Systematic | every k-th item | lists and queues |
Stratified sampling in pandas
Our population is ninety percent from one city, and ten percent from another. A small random sample can easily under count the small city. Group by, then sample a fraction from each group, keeps the shares exact. Run it.
import pandas as pd
pop = pd.DataFrame({"city": ["Big"] * 9000 + ["Small"] * 1000})
random = pop.sample(50, random_state=3)
strat = pop.groupby("city").sample(frac=0.005, random_state=3)
print("random: ", random["city"].value_counts().to_dict())
print("stratified:", strat["city"].value_counts().to_dict())random: {'Big': 46, 'Small': 4}
stratified: {'Big': 45, 'Small': 5}Summary
Random, stratified, cluster, and systematic sampling each fit different needs. Stratifying keeps small groups represented. Next, stratified splits for machine learning.
Key takeaways- Simple random
- Stratified by group
- groupby().sample