0
0
Data Analysis Pythondata~20 mins

groupby() basics in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Groupby Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of groupby() with sum aggregation
What is the output of this code snippet?
Data Analysis Python
import pandas as pd

data = {'Team': ['A', 'A', 'B', 'B', 'C'], 'Points': [10, 15, 10, 5, 20]}
df = pd.DataFrame(data)
result = df.groupby('Team').sum()
print(result)
A
       Points
Team          
A          15
B          5
C          20
B
       Points
Team          
A          10
B          10
C          20
C
       Points
Team          
A          25
B          15
C          20
D
       Points
Team          
A          25
B          10
C          20
Attempts:
2 left
💡 Hint
Sum adds all points for each team.
data_output
intermediate
1:30remaining
Number of groups created by groupby()
How many groups are created by this groupby operation?
Data Analysis Python
import pandas as pd

data = {'Category': ['X', 'Y', 'X', 'Z', 'Y', 'Z', 'Z'], 'Value': [1, 2, 3, 4, 5, 6, 7]}
df = pd.DataFrame(data)
groups = df.groupby('Category')
print(len(groups))
A7
B3
C2
D4
Attempts:
2 left
💡 Hint
Count unique categories in 'Category' column.
🔧 Debug
advanced
2:00remaining
Error raised by incorrect groupby syntax
What error does this code raise?
Data Analysis Python
import pandas as pd

data = {'Type': ['A', 'B', 'A'], 'Score': [5, 10, 15]}
df = pd.DataFrame(data)
result = df.groupby('Type', as_index=False).mean()
print(result)

# Now this incorrect code:
result2 = df.groupby(Type).sum()
ANameError: name 'Type' is not defined
BTypeError: groupby() got an unexpected keyword argument 'Type'
CAttributeError: 'DataFrame' object has no attribute 'Type'
DNo error, prints sum grouped by 'Type'
Attempts:
2 left
💡 Hint
Check if 'Type' is quoted or not in groupby argument.
🚀 Application
advanced
2:00remaining
Using groupby() to find max value per group
Which option correctly finds the maximum 'Sales' value for each 'Region'?
Data Analysis Python
import pandas as pd

data = {'Region': ['East', 'West', 'East', 'West', 'North'], 'Sales': [200, 150, 300, 100, 250]}
df = pd.DataFrame(data)
Adf.groupby('Region')['Sales'].max()
Bdf.groupby('Sales')['Region'].max()
Cdf.groupby('Region').max('Sales')
Ddf.groupby('Region').max()
Attempts:
2 left
💡 Hint
Group by 'Region' and select 'Sales' column before max.
🧠 Conceptual
expert
2:30remaining
Understanding groupby() with multiple aggregation functions
Given this code, what is the shape (rows, columns) of the resulting DataFrame?
Data Analysis Python
import pandas as pd

data = {'Category': ['A', 'A', 'B', 'B', 'C'], 'Value': [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)
result = df.groupby('Category').agg(['min', 'max'])
A(5, 2)
B(5, 1)
C(3, 1)
D(3, 2)
Attempts:
2 left
💡 Hint
Count unique groups and number of aggregation functions.