0
0
Pandasdata~20 mins

Grouping by multiple columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Grouping Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of grouping by two columns and summing
What is the output of this code snippet that groups data by two columns and sums the values?
Pandas
import pandas as pd

data = pd.DataFrame({
    'City': ['NY', 'LA', 'NY', 'LA', 'NY'],
    'Category': ['A', 'A', 'B', 'B', 'A'],
    'Sales': [100, 200, 150, 300, 50]
})
result = data.groupby(['City', 'Category']).sum()
print(result)
A
              Sales
City Category       
LA   A          300
     B          300
NY   A          150
     B          150
B
              Sales
City Category       
LA   A          200
     B          300
NY   A          150
     B          150
C
              Sales
City Category       
LA   A          200
     B          300
NY   A          100
     B          150
D
              Sales
City Category       
LA   A          200
     B          450
NY   A          150
     B          150
Attempts:
2 left
💡 Hint
Think about how groupby sums values for each unique pair of City and Category.
data_output
intermediate
1:30remaining
Number of groups formed by grouping on two columns
Given this DataFrame, how many groups will be formed when grouping by 'Type' and 'Color'?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Type': ['Fruit', 'Fruit', 'Vegetable', 'Fruit', 'Vegetable'],
    'Color': ['Red', 'Green', 'Green', 'Red', 'Red'],
    'Quantity': [10, 15, 7, 5, 3]
})
groups = data.groupby(['Type', 'Color'])
print(len(groups))
A3
B6
C5
D4
Attempts:
2 left
💡 Hint
Count unique pairs of Type and Color in the data.
🔧 Debug
advanced
2:00remaining
Identify the error in grouping by multiple columns
What error will this code raise when trying to group by multiple columns?
Pandas
import pandas as pd

data = pd.DataFrame({
    'A': [1, 2, 1],
    'B': [3, 4, 3],
    'C': [5, 6, 7]
})
result = data.groupby('A', 'B').sum()
print(result)
AValueError: No axis named 'B'
BKeyError: 'B'
CAttributeError: 'DataFrame' object has no attribute 'groupby'
DNo error, outputs grouped sum
Attempts:
2 left
💡 Hint
Check how groupby expects multiple columns as input.
visualization
advanced
2:30remaining
Visualizing grouped data by multiple columns
Which option correctly creates a bar plot showing the sum of 'Value' grouped by 'Group' and 'Subgroup'?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'Group': ['X', 'X', 'Y', 'Y', 'X'],
    'Subgroup': ['A', 'B', 'A', 'B', 'A'],
    'Value': [10, 20, 30, 40, 50]
})
grouped = data.groupby(['Group', 'Subgroup']).sum()

# Which code below plots the grouped data correctly?
A
grouped.plot(kind='bar')
plt.show()
B
grouped.plot(kind='bar', stacked=True)
plt.show()
C
grouped.unstack().plot(kind='bar')
plt.show()
D
data.plot(kind='bar', x='Group', y='Value')
plt.show()
Attempts:
2 left
💡 Hint
Think about how to reshape grouped data for a clear bar plot with multiple categories.
🚀 Application
expert
3:00remaining
Calculate weighted average after grouping by multiple columns
You have this DataFrame with 'Category', 'Subcategory', 'Score', and 'Weight'. How do you calculate the weighted average Score for each Category and Subcategory group?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Category': ['A', 'A', 'B', 'B', 'A'],
    'Subcategory': ['X', 'X', 'Y', 'Y', 'Z'],
    'Score': [80, 90, 70, 60, 85],
    'Weight': [1, 2, 1, 3, 2]
})
Adata.groupby(['Category', 'Subcategory']).apply(lambda x: (x['Score'] * x['Weight']).sum() / x['Weight'].sum())
Bdata.groupby(['Category', 'Subcategory']).mean('Score')
Cdata.groupby(['Category', 'Subcategory']).agg({'Score': 'mean', 'Weight': 'sum'})
Ddata.groupby(['Category', 'Subcategory']).sum()['Score'] / data.groupby(['Category', 'Subcategory']).sum()['Weight']
Attempts:
2 left
💡 Hint
Weighted average is sum of (value * weight) divided by sum of weights.