Bird
Raised Fist0
Pandasdata~20 mins

GroupBy with custom functions in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 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 with a custom aggregation function
What is the output of this code snippet that groups data by 'Category' and applies a custom function to the 'Value' column?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Category': ['A', 'A', 'B', 'B', 'C'],
    'Value': [10, 20, 10, 30, 50]
})

def custom_func(x):
    return x.max() - x.min()

result = data.groupby('Category')['Value'].agg(custom_func)
print(result)
A
Category
A    10
B    20
C     0
Name: Value, dtype: int64
B
Category
A    10
B    10
C    50
Name: Value, dtype: int64
C
Category
A    20
B    30
C    50
Name: Value, dtype: int64
D
Category
A    30
B    40
C    50
Name: Value, dtype: int64
Attempts:
2 left
💡 Hint
Think about what the custom function does: max minus min for each group.
❓ data_output
intermediate
1:30remaining
Number of groups after grouping with a custom function
Given this DataFrame, how many groups are formed when grouping by the 'Type' column?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Type': ['X', 'Y', 'X', 'Z', 'Y', 'Z', 'Z'],
    'Score': [5, 10, 15, 10, 20, 30, 40]
})
groups = data.groupby('Type')

count = len(groups)
A4
B7
C3
D2
Attempts:
2 left
💡 Hint
Count unique values in the 'Type' column.
🔧 Debug
advanced
2:00remaining
Identify the error in custom aggregation function usage
What error does this code raise when trying to apply a custom function to a groupby object?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Group': ['A', 'A', 'B'],
    'Value': [1, 2, 3]
})

def faulty_func(x):
    return x + 1

result = data.groupby('Group').agg(faulty_func)
print(result)
AValueError: Function does not reduce
BAttributeError: 'DataFrameGroupBy' object has no attribute 'agg'
CTypeError: unsupported operand type(s) for +: 'DataFrame' and 'int'
DNo error, prints incremented values
Attempts:
2 left
💡 Hint
Check what the function receives and what operation it tries to do.
❓ visualization
advanced
2:30remaining
Visualizing group means with a custom function
Which option correctly produces a bar plot of the mean 'Score' per 'Category' using a custom aggregation function?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'Category': ['A', 'A', 'B', 'B', 'C'],
    'Score': [10, 20, 30, 40, 50]
})

def mean_func(x):
    return x.mean()

result = data.groupby('Category')['Score'].agg(mean_func)
result.plot(kind='bar')
plt.show()
ANo plot, code raises AttributeError
BA line plot showing scores over categories
CA scatter plot with categories on x-axis and scores on y-axis
DA bar plot showing bars at heights 15, 35, and 50 for categories A, B, and C respectively
Attempts:
2 left
💡 Hint
Check the plot kind and aggregation function used.
🚀 Application
expert
3:00remaining
Applying multiple custom functions with groupby
Given this DataFrame, which option correctly applies two custom functions to the 'Value' column grouped by 'Category' and returns a DataFrame with both results?
Pandas
import pandas as pd

data = pd.DataFrame({
    'Category': ['X', 'X', 'Y', 'Y', 'Z'],
    'Value': [1, 3, 5, 7, 9]
})

def range_func(x):
    return x.max() - x.min()

def mean_plus_one(x):
    return x.mean() + 1

result = data.groupby('Category')['Value'].agg([range_func, mean_plus_one])
print(result)
A
          range_func  mean_plus_one
Category                            
X                  3           2.0
Y                  2           6.0
Z                  1           9.0
B
          range_func  mean_plus_one
Category                            
X                  2           3.0
Y                  2           7.0
Z                  0          10.0
CTypeError: agg() takes 2 positional arguments but 3 were given
DKeyError: 'Value'
Attempts:
2 left
💡 Hint
Check how agg accepts a list of functions and the output format.

Practice

(1/5)
1. What does the groupby function in pandas primarily do?
easy
A. It splits data into groups based on column values for separate analysis.
B. It sorts the entire DataFrame by column values.
C. It deletes duplicate rows from the DataFrame.
D. It merges two DataFrames based on a key column.

Solution

  1. Step 1: Understand the purpose of groupby

    The groupby function divides data into groups based on unique values in one or more columns.
  2. Step 2: Recognize the use of groups

    After grouping, you can apply functions to each group separately for analysis or aggregation.
  3. Final Answer:

    It splits data into groups based on column values for separate analysis. -> Option A
  4. Quick Check:

    Group data = split by column values [OK]
Hint: GroupBy splits data into parts by column values [OK]
Common Mistakes:
  • Confusing groupby with sorting
  • Thinking groupby deletes duplicates
  • Mixing groupby with merge operations
2. Which of the following is the correct syntax to apply a custom function my_func to each group after grouping by column 'Category' in a DataFrame df?
easy
A. df.groupby('Category').apply(my_func)
B. df.apply.groupby('Category')(my_func)
C. df.groupby('Category').my_func()
D. df.group_by('Category').apply(my_func)

Solution

  1. Step 1: Recall pandas groupby syntax

    The correct method to group by a column is df.groupby('ColumnName').
  2. Step 2: Apply a custom function to groups

    Use .apply(function) after grouping to run the function on each group.
  3. Final Answer:

    df.groupby('Category').apply(my_func) -> Option A
  4. Quick Check:

    groupby + apply(custom function) = df.groupby('Category').apply(my_func) [OK]
Hint: Use groupby('col').apply(func) to run custom function [OK]
Common Mistakes:
  • Using wrong method name like group_by
  • Calling function directly on groupby object
  • Misplacing apply before groupby
3. Given the DataFrame df below:
import pandas as pd
data = {'Team': ['A', 'A', 'B', 'B', 'C'], 'Points': [10, 15, 10, 20, 30]}
df = pd.DataFrame(data)

# Custom function to get range of points in group
def point_range(x):
    return x.max() - x.min()

result = df.groupby('Team')['Points'].apply(point_range).to_dict()

What is the value of result?
medium
A. {'A': 10, 'B': 10, 'C': 0}
B. {'A': 25, 'B': 30, 'C': 30}
C. {'A': 5, 'B': 10, 'C': 0}
D. {'A': 15, 'B': 20, 'C': 30}

Solution

  1. Step 1: Group points by 'Team'

    Teams: A has points [10, 15], B has [10, 20], C has [30].
  2. Step 2: Calculate range (max - min) for each group

    For A: 15 - 10 = 5; B: 20 - 10 = 10; C: 30 - 30 = 0.
  3. Final Answer:

    {'A': 5, 'B': 10, 'C': 0} -> Option C
  4. Quick Check:

    Range = max - min per group = {'A': 5, 'B': 10, 'C': 0} [OK]
Hint: Calculate max-min per group for range [OK]
Common Mistakes:
  • Confusing sum with range
  • Forgetting single value group range is zero
  • Mixing keys and values in result
4. Consider this code snippet:
import pandas as pd
data = {'Category': ['X', 'X', 'Y'], 'Value': [1, 2, 3]}
df = pd.DataFrame(data)

# Custom function to sum values
 def sum_values(x):
    return x.sum

result = df.groupby('Category')['Value'].apply(sum_values)

What is the issue with this code?
medium
A. The apply method cannot be used with custom functions.
B. The groupby syntax is incorrect; it should be groupby(['Category']).
C. The DataFrame is missing a required index column.
D. The function sum_values returns the method, not the sum result.

Solution

  1. Step 1: Check the custom function definition

    The function returns x.sum which is a method, not called (missing parentheses).
  2. Step 2: Understand the effect on apply

    Returning the method itself causes the result to be a method object, not the sum value.
  3. Final Answer:

    The function sum_values returns the method, not the sum result. -> Option D
  4. Quick Check:

    Missing () calls method, not result [OK]
Hint: Call sum() with parentheses to get result [OK]
Common Mistakes:
  • Forgetting parentheses on sum()
  • Thinking groupby syntax is wrong here
  • Believing apply can't use custom functions
5. You have a DataFrame df with columns 'Store', 'Product', and 'Sales'. You want to find the difference between the highest and lowest sales per store, but only for products with sales above 100. Which approach correctly uses groupby with a custom function to achieve this?
hard
A. df.groupby('Store')['Sales'].apply(lambda x: x[x > 100].max() - x[x > 100].min())
B. All of the above produce the same correct result.
C. df.groupby('Store').apply(lambda g: g[g['Sales'] > 100]['Sales'].max() - g[g['Sales'] > 100]['Sales'].min())
D. df[df['Sales'] > 100].groupby('Store')['Sales'].apply(lambda x: x.max() - x.min())

Solution

  1. Step 1: Understand filtering sales above 100

    One approach filters inside the apply lambda on each store's sales series. Another filters the DataFrame before grouping. The third filters inside the group apply on the subgroup DataFrame.
  2. Step 2: Check if all approaches calculate max-min correctly

    All three methods correctly compute the difference of max and min sales above 100 per store.
  3. Step 3: Confirm equivalence of results

    Filtering before or inside groupby leads to the same final result for this case.
  4. Final Answer:

    All of the above produce the same correct result. -> Option B
  5. Quick Check:

    Filtering + groupby + custom function = all correct [OK]
Hint: Filter before or inside groupby; both work [OK]
Common Mistakes:
  • Filtering after groupby without apply
  • Not filtering sales > 100 properly
  • Assuming only one method works
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