Phase 22 of 25 · Topic 22.3

Data Aggregation with GroupBy & Aggregations

1Concept

The Split-Apply-Combine strategy: `df.groupby('dept')` splits data into groups; `.agg()` applies aggregations (`sum`, `mean`, `count`, custom lambdas); and combines results back into an aggregated DataFrame.

2Architecture Diagram

Dataset ---> Split by Department ---> Apply mean(salary) per group ---> Combined Summary Table

3Code Example

Python 3.12
# Split-Apply-Combine simulation
transactions = [
    {"dept": "Sales", "amount": 150},
    {"dept": "Eng",   "amount": 400},
    {"dept": "Sales", "amount": 250},
    {"dept": "Eng",   "amount": 350}
]

from collections import defaultdict
totals = defaultdict(int)
for tx in transactions:
    totals[tx["dept"]] += tx["amount"]

print("=== GroupBy Summary Results ===")
for dept, total in totals.items():
    print(f"{dept}: Total Revenue = ${total}")

4Expected Output

=== GroupBy Summary Results ===
Sales: Total Revenue = $400
Eng: Total Revenue = $750

5Key Takeaways

  • Pass dictionaries to `.agg({'col1': 'mean', 'col2': 'sum'})` for multi-column aggregations.
  • Use `as_index=False` in `groupby()` to keep group keys as regular columns instead of DataFrame index.
  • `transform()` returns a Series with the same shape as the original DataFrame (useful for normalizing).