Filtering with Boolean Conditions
In data analysis, you often need to focus on rows that meet specific criteria, such as selecting only rows where sales exceed $100 or users are located in the US.
Pandas makes this simple using boolean conditions.
How It Works
Write a condition that checks whether each row meets your requirement.
The result is a Series of True
or False
values, which Pandas uses to filter the DataFrame.
For example, to filter rows where the value in the "Score"
column is greater than 80
:
df[df["Score"] > 80]
This returns a new DataFrame containing only the rows where the condition is True
.
Why It's Useful
Filtering allows you to:
- Focus on relevant data
- Explore subsets of your dataset
- Prepare data for visualization or modeling
You can also combine conditions using logical operators like &
(AND) and |
(OR). Always wrap each condition in parentheses:
df[(df["Age"] > 30) & (df["Country"] == "Canada")]
This selects rows where both conditions are true.
Summary
- Boolean filtering is a powerful way to isolate rows of interest.
- Use comparison operators like
>
,<
,==
, and!=
for conditions. - Combine multiple conditions with
&
and|
, wrapping each condition in parentheses.
What is the syntax for filtering rows in a DataFrame using a Boolean condition?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help