Understand the idea
The IQR rule flags unusually low or high values for review. It does not establish that a value is wrong.
A small example
If Q1=10 and Q3=14, IQR=4. The fences are 4 and 20: 3 and 21 are flagged; 4 and 20 are not.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
q1 = df["price"].quantile(0.25)
q3 = df["price"].quantile(0.75)
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
df["needs_review"] = (df["price"] < lower) | (df["price"] > upper)
dfWhat each part does
q1 / q3- 25th / 75th percentiles
iqr = q3 - q1- spread of the middle half
1.5 * iqr- distance beyond each quartile for screening
(value < lower) | (value > upper)- flag either tail; retain the original value
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 10 | 4.1 | A |
| Choco Pop | chocolate | 11 | 4.6 | B |
| Mint Bite | mint | 12 | 3.8 | A |
| Berry Loop | fruity | 13 | 4.4 | B |
| Cocoa Cube | chocolate | 14 | 4.9 | A |
| Lemon Drop | fruity | 40 | 4 | B |
Your task · Follow
- In df, add needs_review: True when price is below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR, where IQR = Q3 − Q1.
- Keep all rows and original values; return df.
- Use: quantile().
Hint
The distance extends beyond both quartiles; do not mistake Q3 itself for the upper fence.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
q1 = df["price"].quantile(0.25)
q3 = df["price"].quantile(0.75)
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
df["needs_review"] = (df["price"] < lower) | (df["price"] > upper)
dfOptional stretch
If a sensor is known to saturate at 100, a separate capped column may be defensible. Why should the raw column still be retained?