Set your Publisher ID in src/config/ads.ts to display live ads.
GroupBy & The Split-Apply-Combine Pattern
Group aggregations with agg(), custom lambda summaries, and transform()
The Split-Apply-Combine paradigm groups rows by categorical keys, applies statistical aggregations or transformations, and combines results into a new summary DataFrame.
The agg() and transform() Powerhouses
Split: df.groupby("department") groups rows sharing identical department values.
Apply & Combine: Calling .agg({"salary": ["mean", "median"], "bonus": "sum"}) calculates distinct aggregations for each column simultaneously.
transform(): Applies an aggregation across groups but broadcasts the result back to the original DataFrame row count, allowing row-level comparisons against group averages.
import pandas as pd
sales = pd.DataFrame({
"region": ["East", "West", "East", "West", "East", "West"],
"rep": ["Alice", "Bob", "Charlie", "Diana", "Evan", "Frank"],
"revenue": [5000, 7200, 4800, 8100, 6200, 6900]
})
# Summary report per region
report = sales.groupby("region").agg(
total_revenue=("revenue", "sum"),
average_revenue=("revenue", "mean"),
rep_count=("rep", "count")
)
print("Regional Performance Summary:
", report)Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
What is the main difference between `groupby.agg()` and `groupby.transform()`?