Boolean filtering is the heart of pandas — identical in spirit to NumPy masks. Build a condition that yields True/False per row, then index the DataFrame with it to keep the matching rows.
Boolean filtering
df[df["package"] > 7] # rows where package > 7# Combine with & (and) | (or) — parenthesise each conditiondf[(df["package"] > 6) & (df["programme"] == "Data")]df[df["programme"].isin(["Data", "Web"])] # membershipdf[df["name"].str.startswith("A")] # text condition
The query() alternative
For readability, query() lets you write conditions as a string:
df.query("package > 6 and programme == 'Data'")df.query("package > @threshold") # @ references a Python variable
Key takeaways
- 01Filter rows with a boolean condition: df[df["col"] > x].
- 02Combine conditions with & and | inside parentheses (not and/or).
- 03isin() checks membership; .str methods filter on text.
- 04query("...") is a readable string-based alternative.
