Combinare i filtri con AND e OR
concatenare più condizioni in sicurezza.
Combinare i filtri con AND e OR è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. ✅
Domande Frequenti
La lezione «Combinare i filtri con AND e OR» è gratuita?
Sì — il testo completo di «Combinare i filtri con AND e OR» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.
Cosa imparerò in «Combinare i filtri con AND e OR»?
concatenare più condizioni in sicurezza. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Data Science Academy?
Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Combinare i filtri con AND e OR»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Data Science Academy?
Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Selezionare le colonne per nome
- Filtrare le righe con condizioni
- Combinare i filtri con AND e OR
- query e isin per filtri puliti