Concept[1]
Boolean masks are the core of pandas filtering. A comparison like `df['col'] == value` returns a Series of True/False values. Pass it inside `df[...]` to keep only matching rows.
python
mask = df['country'] == 'France'
filtered_df = df[mask] # Only rows where country is FranceTry it[2]
Get all rows where the country is 'France'.
▸In [ ]:
Quick insert
Concept[3]
Combine multiple conditions with `&` (AND) and `|` (OR). Each condition must be wrapped in parentheses. This is different from Python's `and`/`or` keywords.
python
# AND: both conditions must be true
df[(df['age'] > 25) & (df['city'] == 'Paris')]
# OR: either condition can be true
df[(df['country'] == 'France') | (df['country'] == 'Spain')]Try it[4]
Filter rows where age is greater than 25 AND city is 'Paris'.
▸In [ ]:
Quick insert
Concept[5]
When checking against multiple values, use `.isin()` instead of chaining `|` conditions. It's cleaner and faster for long value lists.
python
# Instead of: (df['color'] == 'red') | (df['color'] == 'blue') | ...
df[df['color'].isin(['red', 'blue', 'green'])]Try it[6]
Keep only rows where the color is 'red', 'blue', or 'green'.
▸In [ ]:
Quick insert