Data Science · Level 2 Filtering

pandas loc Updates

Apply a bulk discount to the right rows with df.loc[mask, column], and avoid the chained assignment that silently changes nothing.

  • df.loc[rows, column]
  • Updating only matching rows
  • Chained assignment doesn't write back

df.loc[mask, "col"] = value changes one column, only in the matching rows. Trap: df[mask]["col"] = value writes into a temporary copy, so df stays the same (pandas warns with SettingWithCopyWarning).

stock = pd.DataFrame({"qty": [0, 8, 0], "status": ["ok", "ok", "ok"]})
stock.loc[stock["qty"] == 0, "status"] = "reorder"
stock["status"].tolist()   # ['reorder', 'ok', 'reorder']

Write bulk_discount(df) that takes 10% off the price of every row with a qty of 10 or more and returns the DataFrame, for example a price of 50 with a qty of 12 becomes 45.0.

solution.py
Test Suites Pending
Output