0
0
Pandasdata~5 mins

Data aggregation reporting in Pandas

Choose your learning style9 modes available
Introduction

Data aggregation reporting helps you combine and summarize data to see the big picture easily.

You want to find the total sales per product in a store.
You need to calculate the average score of students in each class.
You want to count how many times each category appears in your data.
You want to see the maximum temperature recorded each day.
You want to group data by regions and get summary statistics.
Syntax
Pandas
df.groupby('column_name').agg({'column_to_aggregate': 'aggregation_function'})

groupby() splits data into groups based on column values.

agg() applies aggregation like sum, mean, count on grouped data.

Examples
Sum all numeric columns for each category.
Pandas
df.groupby('Category').sum()
Calculate average score for each class.
Pandas
df.groupby('Class').agg({'Score': 'mean'})
Get total and maximum sales per region.
Pandas
df.groupby('Region').agg({'Sales': ['sum', 'max']})
Sample Program

This code groups sales by product and sums the sales for each product.

Pandas
import pandas as pd

data = {'Product': ['Apple', 'Banana', 'Apple', 'Banana', 'Cherry'],
        'Store': ['A', 'A', 'B', 'B', 'A'],
        'Sales': [10, 15, 7, 10, 5]}
df = pd.DataFrame(data)

# Group by Product and sum Sales
report = df.groupby('Product').agg({'Sales': 'sum'})
print(report)
OutputSuccess
Important Notes

You can group by multiple columns by passing a list, e.g., df.groupby(['Product', 'Store']).

Aggregation functions can be customized, like sum, mean, count, max, and min.

After aggregation, the grouped columns become the index of the result.

Summary

Data aggregation helps summarize data by groups.

Use groupby() to split data and agg() to apply summary functions.

Common aggregations include sum, mean, count, max, and min.