Vectorisation over loops
What it is
Operations expressed over whole columns run in compiled code; iterrows and apply execute Python per row.
Why it matters
The gap is routinely one to two orders of magnitude, and it is the difference between a feature job taking seconds and taking an hour.
In the real world: Calculating tax on 10 million orders with one column operation takes a second; a row-by-row loop can take many minutes and holds up the daily report.
# slow: Python once per row
df["margin"] = df.apply(lambda r: r["revenue"] - r["cost"], axis=1)
# fast: one compiled pass
df["margin"] = df["revenue"] - df["cost"]Assuming apply is vectorised because it contains no visible loop. It is a loop with better manners.
Practise it: Sessionise an Event Stream, The Top Three Per Group, Deterministically