Challenge - 5 Problems
Master of Multiple Conditions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of filtering DataFrame with multiple conditions
What is the output DataFrame after applying the filter with multiple conditions using & and | operators?
Pandas
import pandas as pd data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [23, 35, 45, 35, 23], 'Score': [88, 92, 85, 70, 88]} df = pd.DataFrame(data) filtered = df[(df['Age'] > 30) & ((df['Score'] > 80) | (df['Name'] == 'David'))] print(filtered)
Attempts:
2 left
💡 Hint
Remember to use parentheses around each condition when combining with & and | in pandas.
✗ Incorrect
The filter selects rows where Age is greater than 30 AND (Score is greater than 80 OR Name is David). Bob and Charlie have Age > 30 and Score > 80. David has Age > 30 and Name == David, so he is included despite Score being 70.
❓ data_output
intermediate1:30remaining
Count rows matching complex conditions
How many rows in the DataFrame satisfy the condition: (Age < 40) | (Score >= 90) and Name starts with 'A' or 'E'?
Pandas
import pandas as pd data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [23, 35, 45, 35, 23], 'Score': [88, 92, 85, 70, 88]} df = pd.DataFrame(data) condition = ((df['Age'] < 40) | (df['Score'] >= 90)) & (df['Name'].str.startswith(('A','E'))) count = df[condition].shape[0] print(count)
Attempts:
2 left
💡 Hint
Check which names start with A or E first, then apply the combined numeric conditions.
✗ Incorrect
Anna and Eva start with A or E. Both have Age < 40 or Score >= 90. So count is 2.
🔧 Debug
advanced1:30remaining
Identify the error in filtering with multiple conditions
What error does the following code raise when filtering a DataFrame with multiple conditions using & and | without parentheses?
Pandas
import pandas as pd data = {'A': [1, 2, 3], 'B': [4, 5, 6]} df = pd.DataFrame(data) filtered = df[df['A'] > 1 & df['B'] < 6] print(filtered)
Attempts:
2 left
💡 Hint
Operator precedence matters when combining conditions in pandas.
✗ Incorrect
Without parentheses, Python evaluates df['A'] > 1 & df['B'] < 6 as df['A'] > (1 & df['B']) < 6, causing a TypeError because 1 & df['B'] tries to bitwise AND int and Series.
❓ visualization
advanced2:30remaining
Visualize filtered data with multiple conditions
Which plot correctly shows the filtered data points where (Age >= 30) & (Score < 90) from the DataFrame?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [23, 35, 45, 35, 23], 'Score': [88, 92, 85, 70, 88]} df = pd.DataFrame(data) filtered = df[(df['Age'] >= 30) & (df['Score'] < 90)] plt.scatter(filtered['Age'], filtered['Score']) plt.xlabel('Age') plt.ylabel('Score') plt.title('Filtered Data Points') plt.show()
Attempts:
2 left
💡 Hint
Check which rows satisfy Age >= 30 and Score < 90 before plotting.
✗ Incorrect
Rows with Age 35 and Score 85 (Bob), Age 45 and Score 85 (Charlie), and Age 35 and Score 70 (David) satisfy the condition and appear in the scatter plot.
🚀 Application
expert3:00remaining
Apply multiple conditions to categorize data
Given a DataFrame with columns 'Temperature' and 'Humidity', which code correctly creates a new column 'Weather' with values 'Hot & Dry', 'Hot & Humid', 'Cold' based on these conditions?
- 'Hot & Dry' if Temperature > 30 and Humidity < 50
- 'Hot & Humid' if Temperature > 30 and Humidity >= 50
- 'Cold' otherwise
Pandas
import pandas as pd data = {'Temperature': [35, 40, 25, 32, 28], 'Humidity': [45, 55, 60, 40, 70]} df = pd.DataFrame(data) # Fill in the code below
Attempts:
2 left
💡 Hint
Use vectorized conditions with loc for better performance and clarity.
✗ Incorrect
Option A initializes all as 'Cold' then updates rows matching conditions using loc with & and | operators correctly. Option A uses list comprehension but is less efficient. Option A uses apply which is slower. Option A misassigns labels ignoring humidity properly.