Bird
Raised Fist0
Pandasdata~20 mins

Why advanced grouping matters in Pandas - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main benefit of using advanced grouping in pandas?

easy
A. It helps summarize data by multiple categories at once.
B. It makes the data disappear.
C. It deletes duplicate rows automatically.
D. It sorts data alphabetically.

Solution

  1. Step 1: Understand grouping basics

    Grouping in pandas organizes data by categories to summarize it.
  2. Step 2: Recognize advanced grouping advantage

    Advanced grouping uses multiple columns to get deeper insights from data.
  3. Final Answer:

    It helps summarize data by multiple categories at once. -> Option A
  4. Quick Check:

    Advanced grouping = summarize by multiple categories [OK]
Hint: Advanced grouping means grouping by more than one column [OK]
Common Mistakes:
  • Thinking grouping deletes data
  • Confusing grouping with sorting
  • Assuming grouping removes duplicates
2.

Which of the following is the correct syntax to group a DataFrame df by columns 'A' and 'B'?

easy
A. df.group(['A', 'B'])
B. df.groupby(['A', 'B'])
C. df.groupby('A', 'B')
D. df.groupby(['A' & 'B'])

Solution

  1. Step 1: Recall pandas groupby syntax

    The groupby method takes a list of column names inside square brackets.
  2. Step 2: Check each option

    df.groupby(['A', 'B']) uses df.groupby(['A', 'B']) which is correct syntax. Others have wrong method names or wrong argument formats.
  3. Final Answer:

    df.groupby(['A', 'B']) -> Option B
  4. Quick Check:

    Correct groupby syntax uses list of columns [OK]
Hint: Use list inside groupby: df.groupby(['col1', 'col2']) [OK]
Common Mistakes:
  • Using group instead of groupby
  • Passing columns as separate arguments
  • Using bitwise & inside list
3.

Given the DataFrame df:

   Team  Points  Assists
0  A     10      5
1  A     15      7
2  B     10      8
3  B     20      6
4  C     5       9

What is the output of df.groupby('Team').agg({'Points':'sum', 'Assists':'mean'})?

medium
A. Team A: Points=10, Assists=5; B: Points=10, Assists=8; C: Points=5, Assists=9
B. Team A: Points=15, Assists=7; B: Points=20, Assists=6; C: Points=5, Assists=9
C. Team A: Points=25, Assists=6.0; B: Points=30, Assists=7.0; C: Points=5, Assists=9.0
D. Team A: Points=25, Assists=12; B: Points=30, Assists=14; C: Points=5, Assists=9

Solution

  1. Step 1: Sum Points by Team

    Team A: 10+15=25, Team B: 10+20=30, Team C: 5
  2. Step 2: Calculate mean Assists by Team

    Team A: (5+7)/2=6.0, Team B: (8+6)/2=7.0, Team C: 9
  3. Final Answer:

    Team A: Points=25, Assists=6.0; B: Points=30, Assists=7.0; C: Points=5, Assists=9.0 -> Option C
  4. Quick Check:

    Sum and mean aggregation = Team A: Points=25, Assists=6.0; B: Points=30, Assists=7.0; C: Points=5, Assists=9.0 [OK]
Hint: Sum adds values, mean averages them per group [OK]
Common Mistakes:
  • Adding means instead of averaging
  • Mixing sum and mean results
  • Ignoring grouping effect
4.

What is wrong with this code snippet?

df.groupby('Category').agg({'Sales': 'sum', 'Profit'})

Choose the best explanation.

medium
A. The agg method requires a list, not a dictionary.
B. The groupby method cannot take a string argument.
C. The DataFrame must be sorted before grouping.
D. The aggregation dictionary is missing a function for 'Profit'.

Solution

  1. Step 1: Check agg dictionary format

    The agg method expects a dictionary with column names as keys and aggregation functions as values.
  2. Step 2: Identify error in dictionary

    Here, 'Profit' is given without a function, causing a syntax error.
  3. Final Answer:

    The aggregation dictionary is missing a function for 'Profit'. -> Option D
  4. Quick Check:

    agg dict must have column:function pairs [OK]
Hint: agg needs column:function pairs, not just column names [OK]
Common Mistakes:
  • Passing columns without functions in agg
  • Thinking groupby needs sorted data
  • Using wrong argument types in agg
5.

You have a sales DataFrame df with columns 'Region', 'Product', and 'Revenue'. You want to find the total revenue per region and the average revenue per product within each region. Which pandas code correctly achieves this advanced grouping?

hard
A. df.groupby(['Region', 'Product']).agg(TotalRevenue=('Revenue', 'sum')).groupby('Region').agg(TotalRevenue=('TotalRevenue', 'sum'), AvgRevenuePerProduct=('TotalRevenue', 'mean'))
B. df.groupby('Region').agg({'Revenue': 'sum', 'Product': 'mean'})
C. df.groupby('Region').agg(TotalRevenue=('Revenue', 'sum'), AvgRevenuePerProduct=('Revenue', 'mean'))
D. df.groupby(['Region', 'Product']).agg({'Revenue': ['sum', 'mean']})

Solution

  1. Step 1: Calculate total revenue per region and product

    Grouping by both 'Region' and 'Product' and summing 'Revenue' gives total revenue per product in each region.
  2. Step 2: Calculate average revenue per product within each region

    Then grouping by 'Region' again and averaging the summed revenues gives average revenue per product per region.
  3. Step 3: Check options

    df.groupby(['Region', 'Product']).agg(TotalRevenue=('Revenue', 'sum')).groupby('Region').agg(TotalRevenue=('TotalRevenue', 'sum'), AvgRevenuePerProduct=('TotalRevenue', 'mean')) correctly chains these two groupbys to get both metrics separately.
  4. Final Answer:

    df.groupby(['Region', 'Product']).agg(TotalRevenue=('Revenue', 'sum')).groupby('Region').agg(TotalRevenue=('TotalRevenue', 'sum'), AvgRevenuePerProduct=('TotalRevenue', 'mean')) -> Option A
  5. Quick Check:

    Chained groupbys for multi-level aggregation = df.groupby(['Region', 'Product']).agg(TotalRevenue=('Revenue', 'sum')).groupby('Region').agg(TotalRevenue=('TotalRevenue', 'sum'), AvgRevenuePerProduct=('TotalRevenue', 'mean')) [OK]
Hint: Chain groupbys to get totals then averages per group [OK]
Common Mistakes:
  • Trying to get sum and mean in one agg without grouping levels
  • Using mean on non-numeric columns
  • Not chaining groupbys for nested aggregation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard