Group by Several Keys
Multi-level grouping and the result index.
Group by Several Keys is a free Data Science Academy lesson on CoddyKit — lesson 3 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.
Group on More Than One Column
Pass a list of keys to groupby and pandas forms a group for every unique combination of those columns.
df.groupby(["city", "year"])Every Combo Is a Group
Two cities and three years can yield up to six groups, one per existing combination found in your data.
Aggregate Across Keys
Aggregation works exactly the same; the math just runs inside each combination instead of a single key.
df.groupby(["city", "year"])["sales"].sum()Meet the MultiIndex
Grouping on several keys gives the result a MultiIndex, with one index level for each grouping column.
Read a MultiIndex Result
Each output row is labeled by a tuple like (city, year), so the index tells you exactly which group it is.
Select From a MultiIndex
Use loc with a tuple to pull one specific combination straight out of the grouped result.
result.loc[("Paris", 2024)]Flatten With reset_index
Prefer flat columns? reset_index turns each index level back into an ordinary column you can filter.
result.reset_index()Keep Empty Groups Out
By default pandas skips combinations with no rows, so your result shows only combinations that actually occur.
Order of Keys Matters
The key order you list sets the index level order, which changes how the result sorts and reads.
df.groupby(["year", "city"]).size()Unstack for a Cross-Tab
Call unstack to lift one key into columns, turning a long grouped result into a readable grid.
result.unstack()When to Use Several Keys
Reach for multi-key grouping whenever a question mentions two dimensions, like sales by city and by month.
Quick Check
You group by ["city", "year"]. What labels each result row?
Recap: Two Dimensions, One groupby
You can now slice data along several keys at once, read the MultiIndex, and reshape it for clear cross-tabs. 📊
Frequently asked questions
Is the “Group by Several Keys” lesson free?
Yes — the full text of “Group by Several Keys” 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 “Group by Several Keys”?
Multi-level grouping and the result index. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Group by Several Keys” 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.