Create a Derived Column (revenue = qty * unit_price)

Basics
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:

itemqtyunit_price
A310.0
B54.5

Expected Output:

itemqtyunit_pricerevenue
A310.030.0
B54.522.5

Explanation: 3 * 10.0 = 30.0 and 5 * 4.5 = 22.5.

Example 2: Zero Quantity Row

Input:

itemqtyunit_price
C099.0

Expected Output:

itemqtyunit_pricerevenue
C099.00.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
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]