Vectorized Operation
noun
say it: “VEK-tuh-ryzd op-uh-RAY-shun” /ˈvɛktəˌraɪzd ˌɑpəˈreɪʃən/
Math written once for a whole column or array, such as price times quantity, that runs on every value at once with no loop.
What is a vectorized operation?
A vectorized operation is math done on a whole array or column at once, such as prices * 0.9, with no loop of
your own. NumPy runs the loop inside itself, which is much quicker than a Python for loop.
import pandas as pd
orders = pd.DataFrame({"price": [4.50, 6.00], "quantity": [2, 3]})
orders["total"] = orders["price"] * orders["quantity"] # every row at once
print(orders["total"].tolist()) # [9.0, 18.0]
In a sentence
- “The total column came from one vectorized operation: price times quantity.”
- “Swapping the loop for a vectorized operation made the script finish in a second.”
Level: Journeyman · More Python terms at this level
Updated