Get statistics for each group such as count, mean, etc using pandas GroupBy?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
groupby is pandas’ standard tool for computing per-group statistics such as count, mean, min, max, or standard deviation. The basic usage is simple, but real analysis often needs multiple aggregations, renamed outputs, and correct handling of missing values.
The main design choice is how you want the output shaped. For quick exploration, a compact grouped summary is fine. For reusable reports and downstream merges, named aggregations and flattened columns are usually better.
Basic Group Statistics
Start with a simple DataFrame.
This produces one row per group and one column per requested statistic.
Aggregate Multiple Columns
You can summarize several columns at once.
This works, but it produces a multi-level column index. That is fine for analysis, but it can become awkward when exporting or joining the result later.
Named Aggregations Give Cleaner Output
Named aggregation is often the best style for reusable code.
This gives flat column names immediately, which is usually easier for reporting pipelines.
Group by More Than One Key
Real datasets often need grouping on multiple dimensions.
Use reset_index() afterward if you want the keys back as ordinary columns.
Missing Values Change the Meaning
Not all group statistics treat missing values the same way. A common source of confusion is the difference between count and size.
count ignores null values in the selected column. size counts rows regardless of nulls. Choose deliberately based on the reporting definition you need.
Custom Statistics
You can supply your own aggregation function when built-in metrics are not enough.
Custom functions are useful, but they should stay simple and deterministic if you plan to reuse them.
Common Pitfalls
A common mistake is using the default multi-index output and then discovering later that downstream code expected flat column names.
Another issue is confusing count with size in the presence of null values. They answer different questions.
Developers also often forget to call reset_index() before exporting or merging grouped output with another DataFrame.
Finally, custom Python aggregation functions are flexible but can be slower than built-in vectorized operations. Use them when needed, not by default.
It is also easy to forget that group keys may be sorted by default depending on the call pattern. If presentation order matters, sort the result explicitly after aggregation instead of relying on incidental input order.
Summary
- Use pandas
groupbyplusaggto compute per-group statistics. - Named aggregations usually produce the cleanest reusable result shape.
- Grouping by multiple keys is straightforward but may create hierarchical indexes.
- Be explicit about null handling, especially the difference between
countandsize. - Choose output shape intentionally so the summary remains easy to reuse.

