Ravindra BagaleCourses & study guides मराठी Track your progress

21. Stats You Need for Data Work

21.6 Sampling Intuition (Keep It Light)

Why. You rarely analyse every order India-wide in class. You take a sample: one week, six Maharashtra cities, or every 10th row for a quick check. Bad samples mislead — peeking only at Diwali days, or only at Pune.

What.

  • Population (idea): all orders you care about (e.g. Oct 2026 Maharashtra delivered).
  • Sample: the rows you actually load.
  • Bias: sample systematically unlike the population (only evenings, only cancellations).
  • This course does not teach train/test splits or models. We only build the habit: say what your sample is.

How.

Honest sample checklist

  1. Write the date range and cities in the notebook top cell.
  2. Prefer a full week, not only festival days, unless the question is festival-only.
  3. If you downsample for speed: df.sample(n=500, random_state=42) and document it.
  4. Never delete inconvenient rows silently before computing stats.
  5. When sharing: “Based on fictional 120-row Maharashtra extract, 01–30 Oct 2026.”

Worked example A — Biased vs fair peek.

import pandas as pd
import numpy as np

rng = np.random.default_rng(0)
days = np.arange(1, 31)
# fictional daily order counts for Solapur
counts = rng.integers(40, 70, size=30)
counts[14:22] += 50  # Diwali window bump
daily = pd.DataFrame({"day": days, "orders": counts})

biased = daily.loc[daily["day"].between(15, 22), "orders"].mean()
fair = daily["orders"].mean()
print("Diwali-only mean orders/day", round(biased, 1))
print("Full October mean", round(fair, 1))

Biased peek overstates how “busy” Solapur always is.

Worked example B — Reproducible sample.

big = pd.DataFrame({
    "order_id": [f"AMN-{i}" for i in range(1000)],
    "city": rng.choice(
        ["Pune", "Nashik", "Nagpur", "Kolhapur"], size=1000
    ),
    "amount": rng.integers(50, 400, size=1000),
})
sample = big.sample(n=100, random_state=42)
print(sample["city"].value_counts())
print("sample mean ₹", round(sample["amount"].mean(), 1),
      "full mean ₹", round(big["amount"].mean(), 1))

random_state=42 lets Ruhi and Amir get the same 100 rows tomorrow.

Worked example C — What not to do.

Do not open the “interesting” cancelled orders only, compute a cancel rate, and present it as the city cancel rate. That is selection bias — same family of mistake as deleting outliers silently.

Practice task

From any 30-day fictional daily table, compare (a) mean of all days, (b) mean of weekend days only, (c) mean of days 15–22. Write three labels so a manager cannot confuse them.