</>
Skip to content
Pandas lessons (18/42)

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

  1. Filter with conditions
  2. Use isin for multiple values
  3. Use query method
  4. 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.