Pandas Advanced Aggregations

Data SciencePandasFree Lesson

Advertisement

Introduction

Advanced aggregation techniques using groupby with custom functions.

Custom Aggregations

import pandas as pd

def range_func(x):
    return x.max() - x.min()

def coefficient_of_variation(x):
    return x.std() / x.mean()

df.groupby("category").agg({
    "value": [range_func, "mean", "std"]
})

Named Aggregation (Python 3.7+)

df.groupby("category").agg(
    total="value".sum,
    average="value".mean,
    count=("value", "count")
)

Transform

# Add group statistics back to dataframe
df["group_mean"] = df.groupby("category")["value"].transform("mean")
df["group_normalized"] = df["value"] / df["group_mean"]

Filter Groups

# Keep only groups with certain criteria
df.groupby("category").filter(lambda x: x["value"].sum() > 100)

Practice Problems

  1. Create custom aggregation functions
  2. Apply multiple transformations
  3. Filter groups by criteria
  4. Add computed columns to original data
  5. Implement rolling aggregations

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement