5 · Analytics & data science

14. Python for Data Work

NumPy, pandas, vectorisation, Polars and reproducible environments.

11 min read · 3 MCQs

The stack

NumPy provides the array; pandas adds labelled tables; matplotlib and seaborn plot; scikit-learn models; Jupyter explores. Polars and DuckDB have become the fast alternatives for larger-than-memory or performance-critical work.

Vectorise

Loops over rows or .apply() are slow because each call re-enters the Python interpreter. Express operations over whole columns, use groupby aggregations, set correct dtypes (category for low-cardinality strings), and avoid chained assignment which silently writes to a copy.

import pandas as pd

df = pd.read_parquet("orders.parquet")
df["day"] = df["created_at"].dt.floor("D")
df["country"] = df["country"].astype("category")

daily = (df.loc[df["status"].eq("paid")]
           .groupby(["day", "country"], observed=True)
           .agg(revenue=("total_cents", "sum"),
                buyers=("customer_id", "nunique"))
           .reset_index())

daily["revenue"] = daily["revenue"] / 100

Reproducibility

Pin dependencies in a lockfile, seed random number generators, keep notebooks for exploration and move anything recurring into tested modules. A notebook that only runs top-to-bottom on one laptop is not an analysis anyone can trust.

Chapter quiz

3 questions · pass mark 75%
  1. 1. Row-wise .apply() is slow mainly because…

  2. 2. Converting a low-cardinality string column to category…

  3. 3. Reproducible analysis requires…

Answer every question to submit. Progress for da-14 is saved in this browser.