0
0
Pandasdata~5 mins

Named aggregation in Pandas

Choose your learning style9 modes available
Introduction

Named aggregation helps you summarize data by giving clear names to the results. It makes your summary easy to understand and use.

You want to calculate multiple summary statistics on a dataset and keep the results organized.
You need to group data by categories and get clear, named results for each group.
You want to create a summary table with meaningful column names for reports.
You are working with data and want to avoid confusing default column names after aggregation.
Syntax
Pandas
df.groupby('column').agg(
    new_name1=('target_column1', 'agg_func1'),
    new_name2=('target_column2', 'agg_func2')
)

Use new_name to name the output column.

Inside the tuple, first is the column to aggregate, second is the aggregation function like 'sum', 'mean', 'max', etc.

Examples
This groups data by 'Category' and calculates total sales and average price with clear column names.
Pandas
df.groupby('Category').agg(
    total_sales=('Sales', 'sum'),
    average_price=('Price', 'mean')
)
This finds the highest and lowest score for each team with named columns.
Pandas
df.groupby('Team').agg(
    max_score=('Score', 'max'),
    min_score=('Score', 'min')
)
Sample Program

This code groups the sales data by each store. It then calculates the total sales and the average price for each store. The results have clear column names: 'total_sales' and 'average_price'.

Pandas
import pandas as pd

# Create sample data
sales_data = pd.DataFrame({
    'Store': ['A', 'A', 'B', 'B', 'C', 'C'],
    'Product': ['X', 'Y', 'X', 'Y', 'X', 'Y'],
    'Sales': [100, 150, 200, 250, 300, 350],
    'Price': [10, 15, 10, 15, 10, 15]
})

# Group by Store and calculate total sales and average price with named aggregation
summary = sales_data.groupby('Store').agg(
    total_sales=('Sales', 'sum'),
    average_price=('Price', 'mean')
)

print(summary)
OutputSuccess
Important Notes

You can use any aggregation function like 'sum', 'mean', 'max', 'min', 'count', etc.

Named aggregation helps keep your output columns clear and easy to use later.

Summary

Named aggregation lets you give clear names to summary results.

It works well with groupby to summarize data by categories.

Use it to make your data summaries easy to read and understand.