0
0
Data Analysis Pythondata~20 mins

Boolean filtering in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Boolean filtering with pandas
What is the output of this code snippet filtering a DataFrame by a Boolean condition?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
filtered = df[df['A'] > 2]
print(filtered)
A
   A  B
2  3  7
3  4  8
B
   A  B
0  1  5
1  2  6
C
   A  B
1  2  6
2  3  7
D
Empty DataFrame
Columns: [A, B]
Index: []
Attempts:
2 left
💡 Hint
Remember that filtering keeps rows where the condition is True.
data_output
intermediate
1:30remaining
Count of rows after Boolean filtering
After applying this Boolean filter, how many rows remain in the DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'score': [55, 70, 65, 80, 90]})
filtered = df[df['score'] >= 70]
print(len(filtered))
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Count how many scores are 70 or above.
🔧 Debug
advanced
2:00remaining
Identify the error in Boolean filtering code
What error does this code raise when trying to filter a DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'X': [10, 20, 30]})
filtered = df[df['X' > 15]]
print(filtered)
ANo error, outputs filtered DataFrame
BKeyError: True
CSyntaxError: invalid syntax
DTypeError: '>' not supported between instances of 'str' and 'int'
Attempts:
2 left
💡 Hint
Look carefully at the condition inside the brackets.
🚀 Application
advanced
2:30remaining
Filter DataFrame rows with multiple Boolean conditions
Which option correctly filters rows where column 'age' is over 30 and 'income' is at least 50000?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'age': [25, 35, 40, 28], 'income': [40000, 60000, 50000, 45000]})
Adf[(df['age'] > 30) & (df['income'] >= 50000)]
Bdf[df['age'] > 30 and df['income'] >= 50000]
Cdf[(df['age'] > 30) | (df['income'] >= 50000)]
Ddf[df['age'] > 30 or df['income'] >= 50000]
Attempts:
2 left
💡 Hint
Use bitwise operators (&, |) for combining conditions in pandas.
🧠 Conceptual
expert
3:00remaining
Understanding Boolean indexing with missing values
Given a DataFrame with missing values, what does this Boolean filter return?
Data Analysis Python
import pandas as pd
import numpy as np

df = pd.DataFrame({'val': [1, np.nan, 3, np.nan, 5]})
filtered = df[df['val'] > 2]
print(filtered)
AAll rows including NaNs
BRows where 'val' is greater than 2, including NaNs
CRows where 'val' is greater than 2, excluding NaNs
DEmpty DataFrame because NaNs cause errors in filtering
Attempts:
2 left
💡 Hint
NaN compared with any number returns False in Boolean filtering.