Data Science · Level 2 Filtering

Filtering Rows in pandas

Keep the orders from a few cities with a boolean mask and isin(), the pandas way to say "is one of".

  • df[df["col"] > x] keeps matching rows
  • isin() for "one of these"
  • & between conditions

A condition on a column gives a True/False mask, and df[mask] keeps the matching rows. Trap: df["city"] == ["Paris", "Lyon"] doesn't mean "one of these". Use isin().

orders = pd.DataFrame({"city": ["Paris", "Nice", "Lyon"], "total": [80, 20, 30]})
mask = orders["city"].isin(["Paris", "Lyon"])
orders[mask & (orders["total"] > 50)]   # only the Paris order

Write orders_from(df, cities) that returns only the rows whose city is in the list cities, for example with ["Paris"] it keeps the Paris orders.

solution.py
Test Suites Pending
Output