0
0
Pandasdata~5 mins

Boolean indexing in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is Boolean indexing in pandas?
Boolean indexing is a way to select rows or columns in a DataFrame using a condition that returns True or False for each element. It filters data based on these True/False values.
Click to reveal answer
beginner
How do you create a Boolean mask to select rows where a column 'age' is greater than 30?
You write: df['age'] > 30. This creates a Series of True/False values for each row, True if age is greater than 30, else False.
Click to reveal answer
beginner
How do you apply a Boolean mask to a DataFrame to get filtered rows?
Use the mask inside square brackets: df[df['age'] > 30]. This returns only rows where the condition is True.
Click to reveal answer
intermediate
Can Boolean indexing be combined with multiple conditions? How?
Yes. Use & for AND, | for OR, and wrap each condition in parentheses. Example: df[(df['age'] > 30) & (df['city'] == 'NY')]
Click to reveal answer
beginner
What happens if you use Boolean indexing with a condition that has no True values?
The result is an empty DataFrame with the same columns but no rows, because no rows meet the condition.
Click to reveal answer
What does df[df['score'] > 50] return?
ARows where 'score' is greater than 50
BRows where 'score' is less than 50
CAll rows with 'score' equal to 50
DAll rows regardless of 'score'
Which operator is used for AND in multiple Boolean conditions in pandas?
A|
B&&
Cand
D&
What type of object does a Boolean condition like df['age'] > 25 return?
ADataFrame
BList
CBoolean Series
DInteger
What will df[df['name'] == 'Alice'] return if no rows have name 'Alice'?
AError
BEmpty DataFrame
CDataFrame with all rows
DDataFrame with one row
How do you select rows where 'age' is greater than 20 OR 'city' is 'LA'?
Adf[(df['age'] > 20) | (df['city'] == 'LA')]
Bdf[(df['age'] > 20) & (df['city'] == 'LA')]
Cdf[df['age'] > 20 or df['city'] == 'LA']
Ddf[df['age'] > 20 and df['city'] == 'LA']
Explain how Boolean indexing works in pandas and give a simple example.
Think about how you pick items from a list using True/False values.
You got /4 concepts.
    Describe how to combine multiple conditions in Boolean indexing and why parentheses are important.
    Remember operator precedence and how Python evaluates expressions.
    You got /4 concepts.