Pandas — Filtering
Boolean filtering
import pandas as pd
df = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]})
# Single condition
print(df[df['age'] > 25])
# Multiple conditions
print(df[(df['age'] > 25) & (df['age'] < 35)])
isin
df = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'city': ['NYC', 'LA', 'Chicago']})
print(df[df['city'].isin(['NYC', 'LA'])])
query
print(df.query('age > 25'))
print(df.query('age > 25 and city == "NYC"'))
where
print(df.where(df['age'] > 25))
mask
print(df.mask(df['age'] > 25, other=0))
Mini Practice
- Filter with conditions
- Use isin for multiple values
- Use query method
- Apply where and mask
Up Next
Continue with Indexing - Index operations.
Related Topics
Frequently Asked Questions about Filtering
What is Filtering in Pandas?
Filtering is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Filtering?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Filtering.
Why is Filtering important in Pandas?
Filtering is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.