Challenge - 5 Problems
Group Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of group iteration with sum aggregation
What is the output of this code snippet that groups data by 'Category' and sums the 'Value' column?
Pandas
import pandas as pd data = pd.DataFrame({ 'Category': ['A', 'B', 'A', 'B', 'C'], 'Value': [10, 20, 30, 40, 50] }) result = {} for name, group in data.groupby('Category'): result[name] = group['Value'].sum() print(result)
Attempts:
2 left
💡 Hint
Sum the 'Value' for each group named by 'Category'.
✗ Incorrect
The code groups rows by 'Category'. For each group, it sums the 'Value' column. 'A' has 10+30=40, 'B' has 20+40=60, 'C' has 50.
❓ data_output
intermediate1:30remaining
Number of groups created by groupby
How many groups will be created by grouping this DataFrame by 'Type'?
Pandas
import pandas as pd data = pd.DataFrame({ 'Type': ['X', 'Y', 'X', 'Z', 'Y', 'Z', 'Z'], 'Score': [5, 10, 15, 20, 25, 30, 35] }) groups = data.groupby('Type') print(len(groups))
Attempts:
2 left
💡 Hint
Count unique values in 'Type'.
✗ Incorrect
The unique values in 'Type' are 'X', 'Y', and 'Z', so 3 groups are created.
🔧 Debug
advanced2:00remaining
Identify the error in group iteration code
What error will this code raise when trying to iterate over groups?
Pandas
import pandas as pd data = pd.DataFrame({ 'Group': ['G1', 'G2', 'G1'], 'Value': [1, 2, 3] }) for group in data.groupby('Group'): print(group['Value'].sum())
Attempts:
2 left
💡 Hint
Remember what groupby returns when iterated.
✗ Incorrect
data.groupby returns tuples (group_name, group_df). The code tries to index the tuple with a string, causing TypeError.
❓ visualization
advanced2:30remaining
Plotting mean values per group
Which option produces a bar plot showing the mean 'Score' for each 'Category'?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = pd.DataFrame({ 'Category': ['A', 'B', 'A', 'B', 'C'], 'Score': [10, 20, 30, 40, 50] })
Attempts:
2 left
💡 Hint
Group by 'Category', calculate mean, then plot bar chart.
✗ Incorrect
Option D groups by 'Category', calculates mean 'Score', then plots a bar chart. Others either plot wrong data or wrong chart type.
🚀 Application
expert3:00remaining
Extract groups with max average value
Given a DataFrame grouped by 'Team', which code extracts the group(s) with the highest average 'Points'?
Pandas
import pandas as pd data = pd.DataFrame({ 'Team': ['X', 'Y', 'X', 'Z', 'Y', 'Z', 'Z'], 'Points': [10, 20, 30, 40, 50, 60, 70] })
Attempts:
2 left
💡 Hint
Find group means, then filter original data by groups with max mean.
✗ Incorrect
Option B correctly computes mean Points per Team, finds max mean, then filters data for those Teams. Others either use max Points per row or sum instead of mean.