Exercise: Pizza Delivery Report

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.

Journeyman · Python · Updated

By

Crust & Co. Pizza's owner wants a report on the summer's 3,006 delivery orders: which neighborhoods spend the most, how each pizza size does, how the orders split across town, which days are busiest for each size, and how the sales went week by week. Each of those is a question about groups. You'll write the five functions that answer them, one summary table each, and tests call each function and check the table it hands back. A notebook comes with the exercise for looking at the data while you work.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-data-pizza-delivery-report, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-data-pizza-delivery-report.git

Git makes a folder, exercise-data-pizza-delivery-report, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one. Then make the data, once:

python seed.py
Wrote orders.csv: 3006 orders.

The folder holds:

  • seed.py: makes orders.csv, every delivery order from Monday, June 1 to Sunday, August 30, 2026, our own made-up data. Its fixed random seed means everyone gets the same rows. The shop was closed from July 6 to July 12 to replace its oven.
  • pizza.py: read_orders, which loads the orders with ordered_at as dates and times and is ready to use, and the five functions, for you to finish.
  • explore.ipynb: a notebook for looking at the orders and trying your code. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: runs your five functions on the orders and prints what each one found. Ready to use.
  • tests/: the tests, which call each function and check what comes back, and conftest.py, which gives every test its own fresh copy of the orders. You don't need to change them.
  • requirements.txt and pytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

What to do

Every function in pizza.py is already declared with its name and parameter, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should return, with an example:

def sales_by_neighborhood(df):
    """Return each neighborhood's sales, largest first.

    A new DataFrame with two columns, neighborhood and sales (the sum of total for that
    neighborhood, rounded to 2 decimal places), one row per neighborhood, the largest sales
    first, and the rows numbered from 0.

    Elm Park orders of 20.00 and 15.50 and a Westgate order of 40.00:
        neighborhood  sales
    0       Westgate   40.0
    1       Elm Park   35.5
    """
    raise NotImplementedError("Not built yet.")

In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:

  1. sales_by_neighborhood(df) returns two columns, neighborhood and sales (the sum of total, rounded to 2 decimal places), the largest sales first, rows numbered from 0. Elm Park orders of 20.00 and 15.50 and a Westgate order of 40.00 give Westgate 40.0 first, then Elm Park 35.5.
  2. size_summary(df) returns one row per size, in groupby's alphabetical order, with the columns size, orders (a count of order_id), sales (the sum of total, rounded to 2 places) and avg_minutes (the mean of delivery_minutes, rounded to 1 place), built with named aggregation. Two small orders of 10.00 and 14.00 that took 20 and 31 minutes give the small row 2 orders, 24.0 in sales and 25.5 minutes.
  3. share_of_orders(df) returns neighborhood, orders (how many) and percent (that neighborhood's orders divided by all the orders, times 100, rounded to 1 place), the most orders first, rows numbered from 0. Two Elm Park orders and one Hillside order give Elm Park 2 and 66.7, then Hillside 1 and 33.3.
  4. weekday_by_size(df) returns a pivot table counting the orders: rows Monday to Sunday then All, columns small, medium, large, party then All, 0 where a day had no orders of a size, and the totals in All. Two orders on Monday, June 1 (a small and a large) and a small one on Saturday, June 6 make the Monday row 1, 0, 1, 0 and 2, the Saturday row 1, 0, 0, 0 and 1, a Tuesday row of 0s, and 3 in the bottom-right corner.
  5. weekly_sales(df) returns one row for every week from the first order's week to the last, labeled by the Sunday each week ends on, with the columns orders (a count of order_id) and sales (the sum of total, rounded to 2 places). Orders on June 2 (10.00), June 5 (12.50) and June 17 (8.25) give the weeks ending June 7 (2 orders, 22.5), June 14 (0 orders, 0.0) and June 21 (1 order, 8.25).

Each function returns a new table and leaves the one it was given unchanged; the tests check that too. When a function needs an extra column, such as the day of the week, add it to result = df.copy(), not to df.

Check your work

Run the tests from the exercise folder, with the virtual environment on:

python -m pytest

At first all 15 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example sales_by_neighborhood(df)['neighborhood'] for Elm Park 20.00 + 15.50 and Westgate 40.00 should give ['Westgate', 'Elm Park'], but it gave ['Elm Park', 'Westgate'].

To see your functions at work on all the orders, run python report.py.

Hints

  • Look before you write. Run the notebook's cells: value_counts() on neighborhood and size shows what the groups will be.
  • Named columns from the start. groupby(..., as_index=False).agg(sales=("total", "sum")) gives a neighborhood column and a sales column in one step, as Several Summaries at Once: agg shows. Sort with sort_values, then reset_index(drop=True) numbers the rows from 0 again.
  • Round last. Add up or divide first, then round, as Shares and Percentages of a Total explains.
  • The day of the week is a column you make. .dt.day_name() on ordered_at gives each order's day, as Parts of a Date: .dt shows. Then pivot_table with aggfunc="count", fill_value=0 and margins=True, as Pivot Tables and crosstab does, and the reindex line in the docstring puts the rows and columns in order.
  • Weeks need the dates as the index. set_index("ordered_at"), then resample("W"), and agg with named columns works after it, as Totals by Week and Month: resample shows.

When you're done

Every test is green: pytest's last line says 15 passed. And python report.py prints:

1) orders read: 3006
2) best neighborhood: Elm Park 20383.6
3) party orders: 283
4) party average delivery (minutes): 25.7
5) Westgate share of orders (%): 13.6
6) Saturday orders: 584
7) Friday party orders: 51
8) weeks: 13
9) week with the fewest orders: 2026-07-12 0
10) best week's sales: 6727.88

Now look at lines 8 and 9: the summer has 13 weeks, and one of them sold nothing. If the owner asked "what was our average week?", would you count that week? What would you write next to the number, so nobody mistakes the closure for a bad week?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-data-pizza-delivery-report), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-data-pizza-delivery-report.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-data-pizza-delivery-report, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

A report is a handful of grouped questions, each a function you can test: who spends the most, what each kind of order does, how the whole splits up, when the busy days are, and how the weeks went, empty ones included. Written as code, next summer's report is one command.

Key terms

Related