We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Multiple Aggregations

Often you need more than one summary per group: total revenue, average order value, and largest sale all at once. The .agg() method lets you apply multiple aggregation functions in a single call: just pass a list of function names.

orders = pd.DataFrame(
    {
        "category": [
            "Electronics",
            "Electronics",
            "Clothing",
            "Clothing",
            "Electronics",
        ],
        "revenue": [500, 300, 200, 400, 150],
    }
)

summary = orders.groupby("category")["revenue"].agg(["sum", "mean", "max"])
print(summary)
#              sum        mean  max
# category
# Clothing     600  300.000000  400
# Electronics  950  316.666667  500

The result is a DataFrame with one column per aggregation, each named after the function that produced it.

Common Aggregation Functions

These strings work with .agg():

Function What it does
'sum' Total
'mean' Average
'count' Number of non-null values
'size' Number of rows
'min' Minimum value
'max' Maximum value
'std' Standard deviation
'nunique' Number of unique values

count excludes NaN values, while size includes them. If your data has missing values, the difference matters.

Assignment

SnackStack's reliability team wants a temperature health check.

Complete the summarize_temperature_by_type function. It accepts a DataFrame with a temperature_celsius column, and returns one summary row per device type.