Boolean Indexing on DataFrames
Filter rows by applying a boolean condition to a column and combining multiple conditions with & and | operators.
What Is Boolean Indexing?
Boolean indexing lets you filter rows in a DataFrame by applying a condition that produces a Series of True and False values. Only rows where the condition is True are returned. This is one of the most frequently used selection techniques in Pandas because it is both readable and fast.
For example, given a sales DataFrame, you can instantly retrieve all rows where revenue exceeded 1000 without writing a loop.
import pandas as pd
df = pd.DataFrame({
'product': ['A', 'B', 'C', 'D'],
'revenue': [500, 1500, 800, 2000]
})
# Boolean condition produces a Series of True/False
mask = df['revenue'] > 1000
print(mask)
# 0 False
# 1 True
# 2 False
# 3 TrueApplying the Boolean Mask
Once you have a boolean mask, you pass it inside square brackets on the DataFrame to select only the matching rows. The result is a new DataFrame — the original is never modified. The row indices from the original are preserved, so you always know where each row came from.
This pattern — create mask, then apply — is the standard Pandas idiom for row filtering.
import pandas as pd
df = pd.DataFrame({
'product': ['A', 'B', 'C', 'D'],
'revenue': [500, 1500, 800, 2000]
})
mask = df['revenue'] > 1000
filtered = df[mask]
print(filtered)
# product revenue
# 1 B 1500
# 3 D 2000All lessons in this course
- Boolean Indexing on DataFrames
- The query() Method
- isin() and between() Filters
- Selecting Columns by Pattern