Sampling, bias, and representative data
Define the population, sampling unit, and likely reasons that your data omit important cases.
On this page
Define what the data should represent
A population is the group you want to describe, and a sample is the subset observed. Identify the unit before drawing rows. Several line items from one order are dependent, so sampling rows can overrepresent orders with many items.
Measure coverage before estimating
Look for missing stores, time periods, or groups. Stratified sampling draws within defined groups. If group representation in the dataset differs from the target population, an overall estimate may need weights or a narrower claim.
import pandas as pd
orders = pd.DataFrame({"store": ["A", "A", "A", "B", "B"], "total": [4, 7, 6, 8, 9]})
sample = orders.groupby("store").sample(n=1, random_state=42)
print(sample["store"].value_counts().sort_index().to_dict())
Example output
{'A': 1, 'B': 1}