GroupBy and Aggregation
df.groupby(), agg(), transform(), apply(), named aggregations with Named Aggregation.
GroupBy and Aggregation is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.0Multiple Aggregations
Call several reductions at once with .agg([...]), producing a column per function.
print(df.groupby("dept")["salary"].agg(["mean", "min", "max", "count"]))Per-Column Aggregation Dict
Pass a dict to .agg to apply different functions to different columns.
df["bonus"] = [5, 7, 4, 6, 8]
out = df.groupby("dept").agg({"salary": "mean", "bonus": "sum"})
print(out)Named Aggregation
Named aggregation gives output columns clear names using pd.NamedAgg syntax. This avoids confusing multi-level column headers.
out = df.groupby("dept").agg(
avg_salary=("salary", "mean"),
total_bonus=("bonus", "sum"),
)
print(out)Grouping by Multiple Keys
Pass a list of columns to group by several keys at once, producing a hierarchical (MultiIndex) result.
df["level"] = ["jr", "sr", "jr", "sr", "sr"]
print(df.groupby(["dept", "level"])["salary"].mean())transform: Same Shape Out
.transform() returns a result aligned to the ORIGINAL rows, not one row per group. Perfect for adding a group statistic back as a new column.
df["dept_avg"] = df.groupby("dept")["salary"].transform("mean")
print(df[["dept", "salary", "dept_avg"]])transform for Normalization
Because transform broadcasts back to each row, you can normalize within groups, for example subtracting the group mean.
df["centered"] = df["salary"] - df.groupby("dept")["salary"].transform("mean")
print(df[["dept", "salary", "centered"]])apply for Custom Logic
.apply() hands each group (as a sub-DataFrame) to your function. Use it for logic that built-in aggregations cannot express, such as returning the top-N rows per group.
top = df.groupby("dept").apply(lambda g: g.nlargest(1, "salary"))
print(top[["dept", "salary"]])agg vs transform vs apply
agg reduces each group to one value. transform returns one value per original row. apply is the flexible (slower) catch-all. Pick the most specific one for clarity and speed.
Ungrouping with reset_index
GroupBy results put the keys in the index. Call .reset_index() to turn them back into regular columns, which most downstream code expects.
flat = df.groupby("dept")["salary"].mean().reset_index()
print(flat)
# dept salary
# 0 A 60.0
# 1 B 60.0Quick Check
Test your groupby understanding.
Recap
GroupBy essentials:
- Split-apply-combine via
df.groupby("col") .agg([...])and dict/named aggregation for multiple stats.transform()returns original-shaped results.apply()for arbitrary per-group logic- Always
.reset_index()to flatten group keys back to columns
Frequently asked questions
Is the “GroupBy and Aggregation” lesson free?
Yes — the full text of “GroupBy and Aggregation” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “GroupBy and Aggregation”?
df.groupby(), agg(), transform(), apply(), named aggregations with Named Aggregation. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “GroupBy and Aggregation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- GroupBy and Aggregation
- Pivot Tables and Cross-Tabulation
- Advanced Merging and Joining
- Time Series in Pandas