Pandas Grouping and Aggregation

Data SciencePandasFree Lesson

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

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement