BasicsPandasVectorized OpsMetaAmazonUber
Problem
Implement add_revenue_column(df) that returns a copy of df with a new column revenue = qty * unit_price without mutating the input DataFrame.
Why It Matters: Vectorized feature creation (df['a'] * df['b']) runs 100× faster than .iterrows() and is core to feature engineering.
Function Signature
import pandas as pd
def add_revenue_column(df: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Standard Multiplication
Input:
| item | qty | unit_price |
|---|---|---|
| A | 3 | 10.0 |
| B | 5 | 4.5 |
Expected Output:
| item | qty | unit_price | revenue |
|---|---|---|---|
| A | 3 | 10.0 | 30.0 |
| B | 5 | 4.5 | 22.5 |
Explanation: 3 * 10.0 = 30.0 and 5 * 4.5 = 22.5.
Example 2: Zero Quantity Row
Input:
| item | qty | unit_price |
|---|---|---|
| C | 0 | 99.0 |
Expected Output:
| item | qty | unit_price | revenue |
|---|---|---|---|
| C | 0 | 99.0 | 0.0 |
Explanation: Zero quantity cleanly yields revenue = 0.0.
Constraints & Edge Cases
- Do not mutate the input DataFrame in place.
- Start with
out = df.copy()so the caller's DataFrame is not mutated. - Preserve all existing columns and row order.
Constraints & Evaluation Criteria
- Do not mutate the input DataFrame in place.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
qty=[2,5], price=[10.0,3.5]
Expected Output / Criteria
revenue=[20.0, 17.5]