0
0
Pandasdata~20 mins

Iterating over groups in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Group Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A{'A': 40, 'B': 60, 'C': 50}
B{'A': 30, 'B': 60, 'C': 50}
C{'A': 40, 'B': 20, 'C': 50}
D{'A': 40, 'B': 60, 'C': 0}
Attempts:
2 left
💡 Hint
Sum the 'Value' for each group named by 'Category'.
data_output
intermediate
1: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))
A7
B4
C3
D1
Attempts:
2 left
💡 Hint
Count unique values in 'Type'.
🔧 Debug
advanced
2: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())
ATypeError: list indices must be integers or slices, not str
BAttributeError: 'tuple' object has no attribute 'sum'
CKeyError: 'Value'
DNo error, prints sums correctly
Attempts:
2 left
💡 Hint
Remember what groupby returns when iterated.
visualization
advanced
2: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]
})
Adata.groupby('Category')['Score'].sum().plot(kind='line'); plt.show()
Bdata.groupby('Category').plot(kind='bar', y='Score'); plt.show()
Cdata.plot.bar(x='Category', y='Score'); plt.show()
Ddata.groupby('Category')['Score'].mean().plot(kind='bar'); plt.show()
Attempts:
2 left
💡 Hint
Group by 'Category', calculate mean, then plot bar chart.
🚀 Application
expert
3: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]
})
A
result = data.groupby('Team').apply(lambda g: g[g['Points'] == g['Points'].max()])
print(result)
B
means = data.groupby('Team')['Points'].mean()
max_mean = means.max()
result = data[data['Team'].isin(means[means == max_mean].index)]
print(result)
C
means = data.groupby('Team')['Points'].sum()
max_sum = means.max()
result = data[data['Team'] == means.idxmax()]
print(result)
D
max_mean = data['Points'].max()
result = data[data['Points'] == max_mean]
print(result)
Attempts:
2 left
💡 Hint
Find group means, then filter original data by groups with max mean.