Challenge - 5 Problems
Boolean Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Boolean Indexing with Multiple Conditions
What is the output of this code snippet using pandas Boolean indexing?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30, 40, 50], 'B': [5, 15, 25, 35, 45] }) result = df[(df['A'] > 20) & (df['B'] < 40)] print(result)
Attempts:
2 left
💡 Hint
Remember that both conditions must be true for each row to be included.
✗ Incorrect
The code filters rows where column 'A' is greater than 20 and column 'B' is less than 40. Rows with index 2 and 3 satisfy both conditions.
❓ data_output
intermediate1:30remaining
Number of Rows After Boolean Filtering
Given the DataFrame below, how many rows remain after applying the Boolean filter?
Pandas
import pandas as pd df = pd.DataFrame({ 'score': [88, 92, 79, 93, 85], 'passed': [True, True, False, False, False] }) filtered = df[df['passed'] & (df['score'] > 90)] print(len(filtered))
Attempts:
2 left
💡 Hint
Count rows where 'passed' is true and 'score' is greater than 90.
✗ Incorrect
Only one row (index 1 with score 92 and passed true) meets both conditions.
🔧 Debug
advanced2:00remaining
Identify the Error in Boolean Indexing Code
What error does this code raise when executed?
Pandas
import pandas as pd df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}) result = df[(df['X'] > 1) & (df['Y'] < 6)] print(result)
Attempts:
2 left
💡 Hint
Check how to combine multiple Boolean conditions in pandas.
✗ Incorrect
The 'and' operator cannot be used with pandas Series; use '&' instead for element-wise logical AND.
🚀 Application
advanced2:00remaining
Filter DataFrame Rows Based on String Condition
Which option correctly filters rows where the 'city' column contains the substring 'York'?
Pandas
import pandas as pd df = pd.DataFrame({ 'city': ['New York', 'Los Angeles', 'Yorkshire', 'Boston'], 'population': [8000000, 4000000, 500000, 700000] })
Attempts:
2 left
💡 Hint
Use a method that checks if the substring appears anywhere in the string.
✗ Incorrect
str.contains('York') returns true for any string containing 'York', including 'New York' and 'Yorkshire'.
🧠 Conceptual
expert2:30remaining
Understanding Boolean Indexing with Missing Values
Given this DataFrame with missing values, what is the output of the Boolean indexing operation?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 2, np.nan, 4], 'B': [np.nan, 2, 3, 4] }) result = df[df['A'] > 1] print(result)
Attempts:
2 left
💡 Hint
Remember how comparisons with NaN behave in Boolean indexing.
✗ Incorrect
Comparisons with NaN return false, so rows with NaN in 'A' are excluded. Only rows where 'A' > 1 and not NaN are included.