Multiple Aggregations With agg
Different functions per column.
Multiple Aggregations With agg is a free Data Science Academy lesson on CoddyKit — lesson 2 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 Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why One Number Is Not Enough
A single mean hides a lot. With agg you can request several summaries at once and see the fuller picture.
Meet agg
The agg method takes a list of functions and runs every one of them on each group in a single pass.
df.groupby("city")["sales"].agg(["mean", "max"])A List of Function Names
Pass plain strings like "sum" or "min", and pandas maps each to its built-in aggregation for you.
g.agg(["sum", "min", "max"])Each Function Becomes a Column
With a list of functions, the result gains one column per function, neatly side by side for comparison.
Different Functions Per Column
Pass a dictionary to apply different aggregations to different columns in the very same call.
df.groupby("city").agg({"sales": "sum", "age": "mean"})Name Your Output Columns
Named aggregation lets you label results clearly, so you never guess what each output column means.
g.agg(total=("sales", "sum"))Use Your Own Function
Any function returning one value works, so a custom lambda can compute a range or anything you like.
g.agg(lambda s: s.max() - s.min())Mixing Built-ins and Custom
Inside one list you can blend string names with your own callables; pandas applies each to every group.
g.agg(["mean", lambda s: s.std()])Aggregating Many Columns at Once
Skip selecting a column and agg runs across every numeric column, giving a wide summary table.
df.groupby("city").agg("mean")Reset the Index to Flatten
Call reset_index afterward to turn the group keys back into normal columns for easy charting or merging.
result.reset_index()Read the Result Carefully
When functions and columns multiply, you get a MultiIndex on columns; read it as (column, function) pairs.
Quick Check
You want sum on one column and mean on another. What do you pass to agg?
Recap: agg Is Your Swiss Knife
You can now stack many summaries, mix built-ins with custom functions, and name outputs, all through one flexible agg call. 🛠️
Frequently asked questions
Is the “Multiple Aggregations With agg” lesson free?
Yes — the full text of “Multiple Aggregations With agg” is free to read here on the web, and the Data Science Academy 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 Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Multiple Aggregations With agg”?
Different functions per column. You practise Data Science Academy 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 Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multiple Aggregations With agg” 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 Data Science Academy lesson?
Yes. Every Data Science Academy 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
- Split-Apply-Combine Explained
- Multiple Aggregations With agg
- Group by Several Keys
- transform for Group-Wise Features