0
0
Pandasdata~20 mins

Why advanced grouping matters in Pandas - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced Grouping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of groupby with multiple aggregation functions
What is the output of this code snippet using pandas grouping and aggregation?
Pandas
import pandas as pd

data = {'Team': ['A', 'A', 'B', 'B', 'C', 'C'],
        'Points': [10, 15, 10, 20, 10, 30],
        'Assists': [5, 7, 8, 6, 7, 9]}
df = pd.DataFrame(data)

result = df.groupby('Team').agg({'Points': ['sum', 'mean'], 'Assists': 'max'})
print(result)
A
       Points        Assists
          sum  mean      max
Team                          
A          25  12.5        7
B          30  15.0        8
C          40  20.0        9
B
       Points        Assists
          mean  sum      max
Team                          
A          12.5  25        7
B          15.0  30        8
C          20.0  40        9
C
       Points        Assists
          sum  mean      min
Team                          
A          25  12.5        5
B          30  15.0        6
C          40  20.0        7
D
       Points        Assists
          sum  mean      max
Team                          
A          25  12.5        5
B          30  15.0        6
C          40  20.0        7
Attempts:
2 left
💡 Hint
Look carefully at the aggregation functions applied to each column.
data_output
intermediate
1:30remaining
Number of groups after grouping by multiple columns
Given this DataFrame, how many groups will result from grouping by both 'Category' and 'Subcategory'?
Pandas
import pandas as pd

data = {'Category': ['Fruit', 'Fruit', 'Vegetable', 'Vegetable', 'Fruit'],
        'Subcategory': ['Citrus', 'Berry', 'Root', 'Leafy', 'Berry'],
        'Quantity': [10, 15, 7, 8, 5]}
df = pd.DataFrame(data)

groups = df.groupby(['Category', 'Subcategory'])
print(len(groups))
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Count unique pairs of Category and Subcategory.
🔧 Debug
advanced
2:00remaining
Identify the error in this advanced grouping code
What error does this code raise when trying to group and filter a DataFrame?
Pandas
import pandas as pd

data = {'Team': ['X', 'X', 'Y', 'Y'], 'Score': [10, 20, 15, 25]}
df = pd.DataFrame(data)

filtered = df.groupby('Team').filter(lambda x: x['Score'].mean() > 15)
print(filtered)
AKeyError: 'Score' because 'Score' is not accessible inside lambda
BNo error, prints rows where Team's average Score > 15
CTypeError: 'filter' object is not callable
DValueError: The truth value of a DataFrame is ambiguous
Attempts:
2 left
💡 Hint
Check how groupby filter works with lambda functions.
visualization
advanced
1:30remaining
Interpreting a grouped bar chart from grouped data
You have grouped sales data by 'Region' and 'Product' and plotted a grouped bar chart showing total sales. Which statement best describes what the chart shows?
ABars show sales trends over time for each product in all regions.
BBars represent average sales per region stacked by product categories.
CThe chart shows total sales per region with products combined into one bar each.
DEach group of bars shows total sales per product within each region side by side.
Attempts:
2 left
💡 Hint
Grouped bar charts display categories side by side for comparison.
🚀 Application
expert
2:30remaining
Choosing the right grouping method for time series data
You have a DataFrame with timestamps and sales data. You want to analyze weekly sales totals. Which pandas method and parameters will correctly group the data by week?
Pandas
import pandas as pd

# df has columns 'Date' (datetime) and 'Sales'

# Which code correctly groups by week and sums sales?
Adf.groupby('Date').sum().resample('W')
Bdf.groupby('Date').resample('W').sum()
Cdf.groupby(pd.Grouper(key='Date', freq='W')).sum()
Ddf.resample('W').groupby('Date').sum()
Attempts:
2 left
💡 Hint
Use pd.Grouper with freq='W' to group by week on a datetime column.