Boolean indexing is a way to select data from a dataset using True or False values. It helps to filter rows or elements that meet a condition.
Click to reveal answer
beginner
How do you create a Boolean mask in Python with pandas?
You create a Boolean mask by writing a condition on a DataFrame or Series, like df['age'] > 30. This returns True for rows where age is over 30, and False otherwise.
Click to reveal answer
beginner
Example: What does df[df['score'] >= 70] do?
It selects all rows from df where the 'score' column is 70 or higher. This filters the data to only include those rows.
Click to reveal answer
intermediate
Can Boolean indexing be combined with multiple conditions?
Yes! You can combine conditions using & (and), | (or), and ~ (not) with parentheses. For example, df[(df['age'] > 20) & (df['score'] > 50)] selects rows meeting both conditions.
Click to reveal answer
beginner
Why is Boolean indexing useful in data analysis?
It helps quickly find and work with specific parts of data without changing the original dataset. This makes data cleaning and exploration easier and faster.
Click to reveal answer
What does Boolean indexing return when applied to a DataFrame?
AThe original DataFrame unchanged
BA list of column names
CA filtered DataFrame with rows where the condition is True
DA single value from the DataFrame
✗ Incorrect
Boolean indexing filters rows based on True/False conditions, returning only rows where the condition is True.
Which symbol is used for the logical AND in Boolean indexing conditions?
A&
B&&
Cand
D|
✗ Incorrect
In pandas Boolean indexing, & is used for logical AND between conditions.
How do you select rows where the 'age' column is NOT greater than 30?
Adf[df['age'] > 30]
Bdf[~(df['age'] > 30)]
Cdf[df['age'] < 30]
Ddf[df['age'] == 30]
✗ Incorrect
The ~ operator negates the condition, so ~(df['age'] > 30) selects rows where age is NOT greater than 30.
What will df[(df['score'] > 50) | (df['age'] < 20)] select?
ARows where score > 50 OR age < 20
BRows where score > 50 AND age < 20
CRows where score <= 50 AND age >= 20
DRows where score <= 50 OR age >= 20
✗ Incorrect
The | operator means OR, so it selects rows where either condition is true.
What type of object is returned when you write df['age'] > 30?
AA DataFrame
BA list of ages
CAn integer count
DA Series of True/False values
✗ Incorrect
df['age'] > 30 returns a Series of Boolean values indicating which rows meet the condition.
Explain how Boolean indexing works in pandas and give a simple example.
Think about how True/False values help select data.
You got /3 concepts.
Describe how to combine multiple conditions in Boolean indexing and why parentheses are important.
Remember operator precedence and grouping.
You got /3 concepts.
Practice
(1/5)
1. What does Boolean indexing do in data analysis?
easy
A. Selects data based on true or false conditions
B. Sorts data in ascending order
C. Converts data types automatically
D. Merges two datasets together
Solution
Step 1: Understand the purpose of Boolean indexing
Boolean indexing uses true/false values to filter data.
Step 2: Compare with other options
Sorting, converting types, and merging are different operations.
Final Answer:
Selects data based on true or false conditions -> Option A
Quick Check:
Boolean indexing = filter by true/false [OK]
Hint: Boolean indexing filters data by true/false values [OK]
Common Mistakes:
Confusing Boolean indexing with sorting
Thinking it changes data types
Mixing it up with merging datasets
2. Which of the following is the correct syntax to select rows where column 'age' is greater than 30 in a DataFrame df?
easy
A. df['age' > 30]
B. df[df.age = 30]
C. df[df['age'] > 30]
D. df[df['age'] < 30]
Solution
Step 1: Identify correct Boolean indexing syntax
Use df[condition], where condition is df['age'] > 30.
Step 2: Check other options for errors
df['age' > 30] has wrong brackets, C uses assignment (=) instead of comparison, D uses wrong comparison sign.
Final Answer:
df[df['age'] > 30] -> Option C
Quick Check:
Correct syntax uses df[condition] with comparison [OK]
Hint: Use df[condition] with comparison operators for filtering [OK]
Common Mistakes:
Using assignment '=' instead of comparison '>'
Wrong bracket placement
Using incorrect comparison signs
3. Given the DataFrame df below:
import pandas as pd
data = {'score': [85, 42, 78, 90, 55]}
df = pd.DataFrame(data)
What is the output of df[df['score'] >= 78]?
medium
A. Rows with scores 85, 78, and 90
B. Rows with scores 42 and 55
C. All rows because condition is ignored
D. Empty DataFrame
Solution
Step 1: Apply the condition df['score'] >= 78
This selects rows where score is 78 or more: 85, 78, 90.
Step 2: Check which rows match
Rows with scores 42 and 55 do not match, so they are excluded.
Final Answer:
Rows with scores 85, 78, and 90 -> Option A
Quick Check:
Filter keeps scores >= 78 [OK]
Hint: Filter keeps rows where condition is true [OK]
Common Mistakes:
Including rows that don't meet the condition
Thinking condition is ignored
Expecting empty output incorrectly
4. What is wrong with this code snippet?
import pandas as pd
data = {'value': [10, 20, 30, 40]}
df = pd.DataFrame(data)
filtered = df[df.value > 25]
medium
A. It will raise a syntax error
B. It will raise an AttributeError
C. It returns an empty DataFrame
D. It works correctly and filters rows with value > 25
Solution
Step 1: Check attribute access for column 'value'
Using df.value is valid if column name has no spaces or special chars.
Step 2: Verify filtering condition
df.value > 25 selects rows with values 30 and 40 correctly.
Final Answer:
It works correctly and filters rows with value > 25 -> Option D
Quick Check:
Attribute access works for simple column names [OK]
Hint: Use df.column_name if no spaces or special chars [OK]
Common Mistakes:
Assuming df.value always causes error
Expecting syntax error
Thinking it returns empty DataFrame
5. You have a DataFrame df with a column 'status' containing values 'pass', 'fail', and 'pending'. How do you select only rows where 'status' is either 'pass' or 'pending' using Boolean indexing?
hard
A. df[df['status'] == 'pass' and 'pending']
B. df[(df['status'] == 'pass') | (df['status'] == 'pending')]
C. df[df['status'] in ['pass', 'pending']]
D. df[df['status'] == 'pass' or df['status'] == 'pending']
Solution
Step 1: Use element-wise OR for multiple conditions
Use | between conditions inside parentheses for correct Boolean indexing.
Step 2: Avoid Python keywords and wrong syntax
'and' and 'or' do not work element-wise; 'in' is not vectorized here.
Final Answer:
df[(df['status'] == 'pass') | (df['status'] == 'pending')] -> Option B
Quick Check:
Use | with parentheses for multiple conditions [OK]
Hint: Use | with parentheses for multiple conditions [OK]
Common Mistakes:
Using 'and' or 'or' instead of | and &
Trying to use 'in' directly on Series
Missing parentheses around conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.