Combine Filters With AND and OR
Chaining multiple conditions safely.
Combine Filters With AND and OR is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Condition Is Rarely Enough
Real questions stack conditions: adults in Paris, or orders that are late and large. You need to combine filters cleanly. 🧩
Use the Ampersand for AND
To require both conditions, join the two masks with the ampersand. It works element by element across the rows.
df[(df["age"] > 30) & (df["city"] == "Paris")]Use the Pipe for OR
To accept either condition, join the masks with the pipe. A row passes if at least one side is True.
df[(df["city"] == "Paris") | (df["city"] == "Lyon")]Wrap Each Condition in Parentheses
Always put parentheses around each condition. Without them, Python evaluates the operators in the wrong order and raises an error.
df[df["age"] > 30 & df["score"] > 80] # breaksWhy Not and or or?
The plain words and and or work on single True/False values, not on whole Series. For row masks you must use the symbols.
# wrong: df[(df["age"] > 30) and (...)]
# right: use & insteadInvert With the Tilde
The tilde flips a mask, turning True into False. It is the clean way to say not this condition.
df[~(df["city"] == "Paris")] # everyone not in ParisMix AND and OR Carefully
You can blend operators, but parentheses decide the grouping. Make your intent explicit so the logic reads the way you mean it.
df[(df["age"] > 30) & ((c == "Paris") | (c == "Lyon"))]Three or More Conditions
Stacking more masks works the same way. For readability, build them on separate lines, then combine the named masks.
old = df["age"] > 30
top = df["score"] > 80
df[old & top]AND Narrows, OR Widens
Keep the mental model simple: AND shrinks the result toward fewer rows, while OR grows it toward more rows.
Combine Across Columns
Your conditions can touch different columns at once, letting you express rich questions in a single expression.
df[(df["age"] > 30) & (df["active"] == True)]Save Complex Masks
When logic gets dense, give the combined mask a clear name. Future you will thank you for the readable variable.
vip = (df["age"] > 30) & (df["score"] > 80)
df[vip]Quick Check
Pick the expression that filters correctly.
Recap: Combining Filters
Use the ampersand for AND, the pipe for OR, the tilde to invert, and always wrap each condition in parentheses. Complex questions, one clean line. ✅
Frequently asked questions
Is the “Combine Filters With AND and OR” lesson free?
Yes — the full text of “Combine Filters With AND and OR” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Combine Filters With AND and OR”?
Chaining multiple conditions safely. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Combine Filters With AND and OR” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pick Columns by Name
- Filter Rows With Conditions
- Combine Filters With AND and OR
- query and isin for Clean Filters