Ravindra BagaleCourses & study guides

6. Selecting, Filtering, and Sorting

6.2 Boolean Filters

pune = df[df["city"] == "Pune"]
delivered = df[df["status"] == "Delivered"]
big = df[df["amount"] >= 200]

Combine with & | ~ — always wrap each condition in parentheses:

mask = (df["city"] == "Pune") & (df["status"] == "Delivered")
pune_ok = df[mask]
print(pune_ok)

# Cancelled or Returned
bad = df[df["status"].isin(["Cancelled", "Returned"])]
# Not Pune
not_pune = df[~(df["city"] == "Pune")]

Steps in Jupyter

  1. Build df from the sample above.
  2. Filter Delivered orders in Nashik or Nagpur:
    df[(df["status"]=="Delivered") & (df["city"].isin(["Nashik","Nagpur"]))]
  3. Filter amounts between ₹100 and ₹500:
    df[df["amount"].between(100, 500)]
  4. Check .shape after each filter.

What you should see. Pune Delivered rows for Ruhi and Shahrukh; Solapur ₹1,299 in the “big” filter.