0
0
Pandasdata~5 mins

GroupBy with custom functions in Pandas

Choose your learning style9 modes available
Introduction

Grouping data helps us see patterns in smaller parts. Using custom functions lets us get exactly the summary we want.

You want to find the average and also the range (max - min) of sales per store.
You need to count how many unique customers bought each product.
You want to apply a special calculation, like weighted average, on groups of data.
You want to clean or transform data differently for each group before analysis.
Syntax
Pandas
df.groupby('column').agg(custom_function)

# or with multiple columns
# df.groupby('column').agg({'col1': custom_func1, 'col2': custom_func2})

custom_function is a function you define that takes a Series and returns a single value.

You can use built-in functions or write your own for special calculations.

Examples
This example finds the range of sales for each store.
Pandas
def range_func(x):
    return x.max() - x.min()

df.groupby('Store')['Sales'].agg(range_func)
Sum sales and find average quantity sold per product using a lambda function.
Pandas
df.groupby('Product').agg({'Sales': 'sum', 'Quantity': lambda x: x.mean()})
Count unique customers per category.
Pandas
def unique_count(x):
    return x.nunique()

df.groupby('Category')['CustomerID'].agg(unique_count)
Sample Program

This program groups data by store. It calculates the sales range and average quantity sold per store.

Pandas
import pandas as pd

data = {
    'Store': ['A', 'A', 'B', 'B', 'C', 'C'],
    'Sales': [100, 150, 200, 210, 300, 310],
    'Quantity': [1, 2, 2, 3, 3, 4]
}

df = pd.DataFrame(data)

# Custom function to calculate range
def sales_range(x):
    return x.max() - x.min()

# Group by Store and apply custom function on Sales
result = df.groupby('Store').agg({'Sales': sales_range, 'Quantity': 'mean'})

print(result)
OutputSuccess
Important Notes

Custom functions must return a single value for each group.

Use lambda for quick small functions.

GroupBy with custom functions is powerful for tailored summaries.

Summary

Group data to analyze smaller parts.

Use custom functions to get exactly the summary you want.

Combine built-in and custom functions for flexible analysis.