Pandas/Performance & scale 9 min

Vectorisation vs apply

Let pandas do the loop in C instead of in Python.

vectorisednp.whereapply

Column arithmetic runs on whole arrays at once. An apply with a lambda runs your Python function per row and is typically an order of magnitude slower.

python · editable
loading editor…

Try it: Compare a vectorised np.where against an apply with an if/else.

Conditional logic vectorises too: np.where for two branches, np.select for several, and boolean masks with .loc for targeted assignment.

python · editable
loading editor…

Rule of thumb: if you are writing a for loop over rows, or apply(axis=1), there is nearly always a vectorised equivalent.