Challenge - 5 Problems
Master of Selecting Rows by Condition
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of filtering rows with multiple conditions
What is the output of this code snippet that filters rows where age is greater than 30 and score is at least 80?
Pandas
import pandas as pd data = {'name': ['Anna', 'Bob', 'Cara', 'Dan'], 'age': [25, 35, 40, 28], 'score': [88, 75, 90, 85]} df = pd.DataFrame(data) result = df[(df['age'] > 30) & (df['score'] >= 80)] print(result)
Attempts:
2 left
💡 Hint
Remember to use & for 'and' conditions and parentheses around each condition.
✗ Incorrect
The code filters rows where age > 30 and score >= 80. Only Cara (age 40, score 90) meets both conditions.
❓ data_output
intermediate1:30remaining
Number of rows after filtering by string condition
Given this DataFrame, how many rows remain after selecting rows where the 'city' column contains the substring 'York'?
Pandas
import pandas as pd data = {'name': ['Alice', 'Brian', 'Clara', 'David'], 'city': ['New York', 'Yorktown', 'Boston', 'Newark']} df = pd.DataFrame(data) filtered = df[df['city'].str.contains('York')] print(len(filtered))
Attempts:
2 left
💡 Hint
Check which city names include the substring 'York'.
✗ Incorrect
Both 'New York' and 'Yorktown' contain 'York', so 2 rows remain.
🔧 Debug
advanced2:00remaining
Identify the error in filtering rows with OR condition
What error does this code produce when trying to filter rows where 'score' is less than 50 or 'age' is greater than 60?
Pandas
import pandas as pd data = {'name': ['Eve', 'Frank', 'Grace'], 'age': [45, 65, 70], 'score': [55, 40, 30]} df = pd.DataFrame(data) result = df[df['score'] < 50 or df['age'] > 60] print(result)
Attempts:
2 left
💡 Hint
Check how to combine multiple conditions on pandas Series.
✗ Incorrect
Using 'or' between Series causes a ValueError. Use bitwise operator '|' with parentheses instead.
❓ visualization
advanced2:30remaining
Visualizing filtered data distribution
Which plot correctly shows the count of rows where 'category' is 'A' or 'B' from this DataFrame?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'category': ['A', 'B', 'C', 'A', 'B', 'C', 'A'], 'value': [10, 20, 30, 40, 50, 60, 70]} df = pd.DataFrame(data) filtered = df[df['category'].isin(['A', 'B'])] counts = filtered['category'].value_counts() counts.plot(kind='bar') plt.show()
Attempts:
2 left
💡 Hint
Check which categories are included after filtering and what value_counts returns.
✗ Incorrect
Filtering keeps only 'A' and 'B'. value_counts counts 3 for 'A' and 2 for 'B'. The bar chart shows these counts.
🚀 Application
expert3:00remaining
Selecting rows with complex condition and calculating mean
Given this DataFrame, what is the mean 'score' of rows where 'age' is between 20 and 40 (inclusive) and 'passed' is True?
Pandas
import pandas as pd data = {'name': ['Ian', 'Jane', 'Kyle', 'Lara', 'Mona'], 'age': [22, 35, 41, 28, 39], 'score': [88, 92, 85, 79, 95], 'passed': [True, False, True, True, False]} df = pd.DataFrame(data) filtered = df[(df['age'] >= 20) & (df['age'] <= 40) & (df['passed'])] mean_score = filtered['score'].mean() print(round(mean_score, 2))
Attempts:
2 left
💡 Hint
Filter rows carefully, then calculate the average of the 'score' column.
✗ Incorrect
Rows matching conditions: Ian (22, True, 88), Lara (28, True, 79). Mean score = (88 + 79) / 2 = 83.5.