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
- Build
dffrom the sample above. - Filter Delivered orders in Nashik or Nagpur:
df[(df["status"]=="Delivered") & (df["city"].isin(["Nashik","Nagpur"]))] - Filter amounts between ₹100 and ₹500:
df[df["amount"].between(100, 500)] - Check
.shapeafter each filter.
What you should see. Pune Delivered rows for Ruhi and Shahrukh; Solapur ₹1,299 in the “big” filter.