0
0
Pandasdata~20 mins

groupby() basics in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Groupby Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of groupby sum aggregation
What is the output of this code?
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()
Pandas
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
  Team  Points
0    A      25
1    B      15
2    C      20
B
       Points
Team          
A          25
B          15
C          20
C
Team
A    25
B    15
C    20
Name: Points, dtype: int64
D
       Points
Team          
A          10
B          10
C          20
Attempts:
2 left
💡 Hint
Remember groupby groups rows by the 'Team' column and sums the 'Points' for each group.
data_output
intermediate
1:30remaining
Number of groups created by groupby
Given this DataFrame, how many groups will be created by grouping on 'Category' column?
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')
Pandas
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))
A3
B7
C4
D2
Attempts:
2 left
💡 Hint
Count unique values in the 'Category' column.
🔧 Debug
advanced
1:30remaining
Identify the error in groupby usage
What error does this code raise?
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Cara'], 'Score': [90, 80, 85]}
df = pd.DataFrame(data)
result = df.groupby('Age').mean()
Pandas
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Cara'], 'Score': [90, 80, 85]}
df = pd.DataFrame(data)
result = df.groupby('Age').mean()
ATypeError: unsupported operand type(s) for +: 'int' and 'str'
BValueError: No numeric types to aggregate
CKeyError: 'Age'
DAttributeError: 'DataFrame' object has no attribute 'mean'
Attempts:
2 left
💡 Hint
Check if the column used in groupby exists in the DataFrame.
🚀 Application
advanced
2:00remaining
Calculate average sales per region
You have sales data:
import pandas as pd

data = {'Region': ['North', 'South', 'North', 'East', 'South', 'East'], 'Sales': [100, 200, 150, 300, 250, 350]}
df = pd.DataFrame(data)

Which code correctly calculates the average sales per region?
Pandas
import pandas as pd

data = {'Region': ['North', 'South', 'North', 'East', 'South', 'East'], 'Sales': [100, 200, 150, 300, 250, 350]}
df = pd.DataFrame(data)
Adf.groupby('Region').count()['Sales']
Bdf.groupby('Sales')['Region'].mean()
Cdf.groupby('Region').sum()['Sales'] / df['Sales'].count()
Ddf.groupby('Region')['Sales'].mean()
Attempts:
2 left
💡 Hint
Group by 'Region' and then calculate mean of 'Sales'.
🧠 Conceptual
expert
1:30remaining
Understanding groupby object behavior
Which statement about a pandas groupby object is TRUE?
AA groupby object is an iterable that yields (group_name, DataFrame) pairs.
BA groupby object immediately computes aggregation when created.
CA groupby object can be indexed like a DataFrame to get groups directly.
DA groupby object stores the original DataFrame as a list of groups.
Attempts:
2 left
💡 Hint
Think about what happens when you loop over a groupby object.