0Pricing
Learn AI with Python · Lesson

GroupBy and Aggregation

df.groupby(), agg(), transform(), apply(), named aggregations with Named Aggregation.

The Split-Apply-Combine Idea

GroupBy follows the split-apply-combine pattern: split rows into groups by a key, apply a function to each group, then combine the results into one frame. It powers most analytical summaries.

import pandas as pd
df = pd.DataFrame({
    "dept": ["A", "A", "B", "B", "B"],
    "salary": [50, 70, 40, 60, 80],
})

Basic groupby

df.groupby("col") creates a grouped object. Chaining an aggregation like .mean() computes one value per group.

print(df.groupby("dept")["salary"].mean())
# dept
# A    60.0
# B    60.0

All lessons in this course

  1. GroupBy and Aggregation
  2. Pivot Tables and Cross-Tabulation
  3. Advanced Merging and Joining
  4. Time Series in Pandas
← Back to Learn AI with Python