Bucket Numeric Spend Into Low / Medium / High Tiers

Basics
BasicsPandasBinningMetaAmazonUber

Problem

Implement add_spend_tier(df) returning a copy of df with a new tier column: 'Low' if spend < 50, 'Medium' if 50 <= spend < 200, and 'High' if spend >= 200.

Why It Matters: Vectorized bucketing with np.select or pd.cut creates interpretable customer segments without slow row-by-row .apply() loops.

Function Signature

import numpy as np
import pandas as pd

def add_spend_tier(df: pd.DataFrame) -> pd.DataFrame:

Sample Input & Output

Example 1: All Three Tiers + Exact Boundaries

Input:

userspend
A49.9
B50.0
C199.9
D200.0

Expected Output:

userspendtier
A49.9Low
B50.0Medium
C199.9Medium
D200.0High

Explanation: 49.9 -> Low, 50.0 -> Medium, 199.9 -> Medium, 200.0 -> High.

Example 2: Zero Spend

Input:

userspend
Z0.0

Expected Output:

userspendtier
Z0.0Low

Explanation: 0.0 < 50 maps to 'Low'.

Constraints & Edge Cases

  • < 50 -> 'Low', 50 <= x < 200 -> 'Medium', >= 200 -> 'High'.
  • Exact boundary checks: 50 is 'Medium' and 200 is 'High'.
  • Do not mutate df in place.

Constraints & Evaluation Criteria

  • `< 50` -> `'Low'`, `50 <= x < 200` -> `'Medium'`, `>= 200` -> `'High'`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[20, 50, 199.9, 200]
Expected Output / Criteria
['Low','Medium','Medium','High']