Python
#19

Vectorization vs apply() vs iterrows(): Performance Benchmarks in Pandas

MediumGoogle
Interview Question #19
Asked at Google

Given a DataFrame with 1 million rows containing `quantity` and `unit_price`, compute `total_revenue = quantity * unit_price`. Compare the performance of `iterrows()`, `.apply(axis=1)`, and direct vectorization.

Input Table: sales_1m
1 rows preview
rowsquantityunit_price
1,000,000 rows520
Expected Output Structure3 rows
methodruntime
df['q'] * df['p'] (Vectorized)3 ms (Fastest)
df.apply(axis=1)1,200 ms
for row in df.iterrows()28,000 ms (Slowest)
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

Python 3.10 (Pandas)
Environment Ready

Click "Run & Test" to execute your Pandas code against the test assertions.

Chat with us