Interview Q&A / Data Science

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Why is apply slower than vectorised operations in pandas?

SHORT ANSWER

apply runs a Python function once per row or element, paying Python overhead each time. Vectorised operations such as column arithmetic or str methods run in compiled code on whole arrays, and are often tens of times faster.

Explain it simply

A vectorised operation is a printing press: set it once and the whole page is done. apply is a clerk copying each line by hand. Both give the same answer, but one is far faster on large data.

Example

python
import pandas as pd
x = pd.Series([1, 2, 3, 4])
print((x * 2).tolist())              # [2, 4, 6, 8]
print(x.apply(lambda v: v * 2).tolist())  # [2, 4, 6, 8]

Same output, very different speed once you have a million rows. Better tools than apply for common jobs:

TaskVectorised choice
Arithmetic on columnsdf["a"] + df["b"]
If-else on a columnnp.where(df["x"] > 0, "pos", "neg")
Several conditionsnp.select(conditions, choices)
Text cleanupdf["name"].str.strip().str.lower()
Datesdf["d"].dt.month

Use apply when the logic is genuinely complex or needs several columns in a way that has no vectorised form, and accept the cost.

What to say in the interview

"apply loops in Python, so it scales poorly. I prefer built-in column operations, np.where, np.select, the str and dt accessors, or map for dictionary lookups. I reach for apply only as a last resort and avoid axis=1 apply on large frames."

Common mistakes

  • Writing a row-wise apply for something a single column expression can do.

#pandas#performance#vectorisation

Related questions

Practise more Data Science questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Data Science quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.