Boolean indexing is a filtering technique in Pandas that selects rows or columns based on boolean conditions (True or False). Instead of using row labels or integer positions, it retrieves data by evaluating conditions on column values or index values.
- Accessing a DataFrame with a boolean index
- Applying a boolean mask to a dataframe
- Masking data based on column value
- Masking data based on an index value
Example: Filter Rows Based on a Condition
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(df[df["score"] > 80])
Output
name score 0 Aparna 90 3 Geeku 98
Filtering Data Using Comparison Operators
Boolean indexing can be performed using comparison operators such as ==, !=, >, <, >=, and <=..
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"degree": ["BCA", "BCA", "M.Tech", "BCA"],
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(df[df["degree"] == "BCA"])
Output
name degree score 0 Aparna BCA 90 1 Pankaj BCA 40 3 Geeku BCA 98
Explanation:
- df["degree"] == "BCA" creates a boolean Series.
- df[...] returns rows where the condition is True.
Using Multiple Conditions
Multiple conditions can be combined using logical operators such as & (AND), | (OR), and ~ (NOT).
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"degree": ["MBA", "BCA", "M.Tech", "MBA"],
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(
df[
(df["degree"] == "MBA")
& (df["score"] > 90)
]
)
Output
name degree score 3 Geeku MBA 98
Explanation:
- & combines multiple conditions.
- Parentheses are required around each condition.
- Only rows satisfying both conditions are returned.
Using .loc[] with Boolean Conditions
.loc[] can be combined with boolean conditions to create more readable filtering operations.
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(
df.loc[
df["score"] > 80
]
)
Output
name score 0 Aparna 90 3 Geeku 98
Explanation:
- df["score"] > 80 creates a boolean Series.
- .loc[] selects rows where the condition is True.
Filtering Using .isin()
.isin() checks whether values belong to a given list.
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"degree": ["MBA", "BCA", "M.Tech", "MBA"]
}
df = pd.DataFrame(data)
print(
df[
df["degree"].isin(
["MBA", "M.Tech"]
)
]
)
Output
name degree 0 Aparna MBA 2 Sudhir M.Tech 3 Geeku MBA
Explanation:
- .isin() compares values against multiple options.
- It returns a boolean Series used for filtering.
Filtering a Range of Values Using .between()
.between() filters rows whose values fall within a specified range.
import pandas as pd
data = {
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(
df[
df["score"].between(
80,
95
)
]
)
Output
score 0 90 2 80
Explanation:
- .between(start, end) checks whether values lie within a range.
- It returns a boolean Series for filtering.
Applying a Boolean Mask to a DataFrame
A boolean mask is a list or Series containing True and False values. Rows corresponding to True are selected.
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"],
"score": [90, 40, 80, 98]
}
df = pd.DataFrame(data)
print(
df[
[True, False, True, False]
]
)
Output
name score 0 Aparna 90 2 Sudhir 80
Explanation:
- The boolean list must have the same length as the DataFrame.
- Rows corresponding to True are returned.
Filtering Based on Index Values
Boolean indexing can also filter rows using index values.
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"]
}
df = pd.DataFrame(data)
mask = df.index > 1
print(df[mask])
Output
name 2 Sudhir 3 Geeku
Explanation:
- df.index > 1 creates a boolean mask from index values.
- df[mask] returns rows whose index is greater than 1.
Boolean Index Labels
Pandas also allows boolean values (True and False) to be used as row labels in the index.
import pandas as pd
data = {
"name": ["Aparna", "Pankaj", "Sudhir", "Geeku"]
}
df = pd.DataFrame(
data,
index=[True, False, True, False]
)
print(df.loc[True])
Output
name True Aparna True Sudhir
Explanation:
Boolean index labels are different from Boolean indexing. Instead of using True and False to filter rows based on conditions, the DataFrame itself uses boolean values as row labels in its index.
- index=[True, False, True, False] creates a DataFrame whose row labels are boolean values.
- df.loc[True] selects all rows whose index label is True.
- Similarly, df.loc[False] selects all rows whose index label is False.
- This technique is rarely used in practice and should not be confused with Boolean indexing, which filters rows using conditions.
Deprecated Methods
Some older Pandas methods related to indexing are deprecated and should not be used in modern code.
Example: The .ix[] indexer was a hybrid of .loc[] and .iloc[] that supported both label-based and integer-based indexing. It was deprecated in Pandas 0.20.0 and removed in Pandas 1.0 due to ambiguous behavior.
Avoid using:
df.ix[1]
df.ix["name"]
Use .loc[] for label-based indexing:
df.loc[1]
Use .iloc[] for integer-based indexing:
df.iloc[1]
Indexing Methods
| Method | Purpose |
|---|---|
| df.loc[] | Label-based indexing |
df.iloc[] | Integer position-based indexing |
df[] | Column selection and boolean filtering |
df.at[] | Fast access to a single value by label |
df.iat[] | Fast access to a single value by integer position |