← Back to Python

All Topics

Advertisement

Learn/Python/Data Science

Pandas Grouping and Aggregation

Topic: Pandas

Advertisement

Introduction

GroupBy operations allow splitting data into groups, applying functions, and combining results.

Basic GroupBy

df = pd.DataFrame({
    "category": ["A", "B", "A", "B", "A"],
    "value": [10, 20, 15, 25, 30]
})

# Group by single column
grouped = df.groupby("category")

# Aggregate functions
grouped["value"].sum()
grouped["value"].mean()
grouped["value"].agg(["sum", "mean", "count"])

Multiple Aggregations

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

# Custom aggregation
def range_func(x):
    return x.max() - x.min()

df.groupby("category")["value"].agg(range_func)

GroupBy with Transformation

# Normalize within groups
df["normalized"] = df.groupby("category")["value"].transform(
    lambda x: (x - x.mean()) / x.std()
)

Practice Problems

  1. Calculate total sales by region
  2. Find average temperature by month
  3. Count occurrences per category
  4. Compute percentage of total within groups
  5. Apply multiple aggregation functions

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →