# Grouping and Summarizing

Most questions a manager asks are about groups: which neighborhood orders the most, which pizza size sells best on Fridays. Using a pizza shop's delivery orders, group and summarize with groupby and agg, work out shares of a total, build pivot tables, and total sales by week and month.

Updated: November 19, 2026

## [Grouping](https://www.ask-codey.com/courses/python-data-analysis/topics/data-grouping)

1. [Grouping with groupby](https://www.ask-codey.com/lessons/pandas-groupby) (Apprentice): groupby sorts a table's rows into groups that share a value, such as a neighborhood, works out one number for each group (a sum, a mean, a count) and hands back one row per group, which answers "which one sold the most?" in a line instead of a filter for every group.
2. [Several Summaries at Once: agg](https://www.ask-codey.com/lessons/pandas-groupby-agg) (Journeyman): agg works out several summaries for each group in one table, from a list such as \['sum', 'mean', 'count'\] or by named aggregation, which names each new column and the column and summary it comes from; groupby takes a list of columns to group by two things at once, and reset_index or as_index=False turns the group labels back into ordinary columns.
3. [Shares and Percentages of a Total](https://www.ask-codey.com/lessons/pandas-shares-of-a-total) (Journeyman): A group's total means more beside the whole, so divide each group's total by the grand total and multiply by 100 for its share as a percentage; transform('sum') lines each group's total up with every row, so each row's share of its own group is one division; and shares are rounded to one decimal place only at the end, for people to read.

## [Pivot Tables](https://www.ask-codey.com/courses/python-data-analysis/topics/data-pivot-tables)

1. [Pivot Tables and crosstab](https://www.ask-codey.com/lessons/pandas-pivot-table) (Journeyman): pivot_table turns a summary of two columns into a grid, one value down the side (index) and one across the top (columns), with the cells worked out from values by aggfunc; fill_value puts 0 in an empty cell, margins adds the totals, and crosstab counts every combination of two columns, as row percentages with normalize='index'.

## [Totals by Week and Month](https://www.ask-codey.com/courses/python-data-analysis/topics/data-time-periods)

1. [Totals by Week and Month: resample](https://www.ask-codey.com/lessons/pandas-resample) (Journeyman): Total a column by month with groupby on dt.to_period('M'), and by week with resample('W') on a date index, which labels each week by the Sunday it ends on and keeps every week from the first to the last, so a week with no orders shows as 0 instead of disappearing, while its mean is NaN.

## [Exercise: Pizza Delivery Report](https://www.ask-codey.com/courses/python-data-analysis/topics/exercise-data-pizza-delivery-report)

1. [Exercise: Pizza Delivery Report](https://www.ask-codey.com/lessons/exercise-data-pizza-delivery-report) (Journeyman): Write five summary functions for a pizza shop's summer of delivery orders, sales by neighborhood, a summary of each size by named aggregation, each neighborhood's share of the orders, a weekday-by-size pivot table with totals and the sales week by week, and let pytest check the table each one returns.

Source: https://www.ask-codey.com/courses/python-data-analysis/chapters/data-grouping-and-summarizing
