Recall & Review
beginner
What does the
groupby() function do in pandas?It splits the data into groups based on one or more columns, so you can perform operations on each group separately.
Click to reveal answer
beginner
How can you apply a custom function to each group in pandas?
Use the
.apply() method after groupby() to run your own function on each group.Click to reveal answer
intermediate
What is the difference between
.agg() and .apply() in groupby?.agg() is for simple aggregation functions like sum or mean, while .apply() lets you run any custom function on groups.Click to reveal answer
beginner
Write a simple custom function to calculate the range (max - min) of a group.
def range_func(x):
return x.max() - x.min()
Click to reveal answer
intermediate
Why use custom functions with groupby instead of built-in aggregations?
Custom functions let you do special calculations or transformations that built-in functions don’t support.Click to reveal answer
Which pandas method allows you to run your own function on each group after grouping?
✗ Incorrect
The .apply() method runs a custom function on each group after using groupby.
What does this custom function do?
def range_func(x):
return x.max() - x.min()✗ Incorrect
The function returns the range (max - min) of values in the group.
Which of these is NOT a reason to use a custom function with groupby?
✗ Incorrect
Sorting groups alphabetically is not a reason to use custom functions; it's handled separately.
What will this code do?
df.groupby('Category')['Value'].apply(lambda x: x.sum())✗ Incorrect
It sums the 'Value' column for each group defined by 'Category'.
Which pandas method is best for simple aggregations like mean or sum after groupby?
✗ Incorrect
.agg() is designed for simple aggregation functions like mean or sum.
Explain how to use a custom function with pandas groupby to calculate a special metric for each group.
Think about splitting data, defining a function, and applying it.
You got /4 concepts.
Describe the difference between using .agg() and .apply() after grouping data in pandas.
One is for simple aggregations, the other for flexible custom functions.
You got /4 concepts.