Python · medium
Asked in Data Scientist interviews, in the Python round.
Vectorized ops are fastest; map is element-wise on Series; apply is flexible but slower (row/col).
Three ways to transform a column, at three speeds.
df["x"] * 2, np.log(df["x"]), df["s"].str.lower()): run in compiled code over the whole array. Fastest by one to two orders of magnitude. Use them whenever one exists.Series.map(f): element by element on one Series. Takes a function, a dict, or a Series; a dict is a fast lookup-table substitution.apply(f): the general escape hatch. On a Series it is like map; on a DataFrame with axis=1 it calls f once per row with a Series argument, which is the slowest thing you can do in pandas.df["cents"] = df["price"] * 100 # vectorized
df["region"] = df["country"].map(COUNTRY_TO_REGION) # dict lookup
df["label"] = df.apply(lambda r: f"{r.sku}-{r.size}", axis=1) # slow; avoid at scale
df["label"] = df["sku"] + "-" + df["size"] # the same, vectorized
The rule: reach for apply(axis=1) only when the logic genuinely needs several columns and cannot be expressed with np.where, np.select, or arithmetic. On a million rows the difference is seconds versus minutes.
apply(axis=1) is a Python loop in disguise.apply for arithmetic because it "reads clearer", then wondering why the pipeline takes ten minutes.