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:
| user | spend |
|---|---|
| A | 49.9 |
| B | 50.0 |
| C | 199.9 |
| D | 200.0 |
Expected Output:
| user | spend | tier |
|---|---|---|
| A | 49.9 | Low |
| B | 50.0 | Medium |
| C | 199.9 | Medium |
| D | 200.0 | High |
Explanation: 49.9 -> Low, 50.0 -> Medium, 199.9 -> Medium, 200.0 -> High.
Example 2: Zero Spend
Input:
| user | spend |
|---|---|
| Z | 0.0 |
Expected Output:
| user | spend | tier |
|---|---|---|
| Z | 0.0 | Low |
Explanation: 0.0 < 50 maps to 'Low'.
Constraints & Edge Cases
< 50->'Low',50 <= x < 200->'Medium',>= 200->'High'.- Exact boundary checks:
50is'Medium'and200is'High'. - Do not mutate
dfin place.
Constraints & Evaluation Criteria
- `< 50` -> `'Low'`, `50 <= x < 200` -> `'Medium'`, `>= 200` -> `'High'`.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[20, 50, 199.9, 200]
Expected Output / Criteria
['Low','Medium','Medium','High']