Exercise: Farmers Market First Look

Write five functions that load a farmers market's sales from a CSV file, its vendor list from the right sheet of an Excel workbook and its nested JSON reviews as one row each, then sum up a table and count its top categories, and let pytest check what each one returns.

Apprentice · Python · Updated

By

Sunnyside Farmers Market's coordinator wants a first look at the season before the winter planning meeting, and the data arrives three ways: the sales as a CSV file, the vendor list as an Excel workbook with a sheet for each year and two title rows on each, and the reviews from the market's app as JSON with each vendor's reviews nested inside it. You'll write the five functions that load each file into a pandas table and sum a table up, and tests call each function and check what 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-farmers-market-first-look, 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-farmers-market-first-look.git

Git makes a folder, exercise-data-farmers-market-first-look, 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 sales.csv: 1900 rows.
Wrote vendors.xlsx: sheets 2025 and 2026.
Wrote reviews.json: 15 vendors, 104 reviews.

The folder holds:

  • seed.py: makes the market's three files, our own made-up data. Its fixed random seed means everyone gets the same rows.
  • market.py: the five functions, for you to finish.
  • explore.ipynb: a notebook for looking at the files 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: loads the three files with your five functions and prints what's in them. 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 files. 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 market.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do, with examples:

def top_categories(sales, n):
    """Return a Series of the n categories that appear in the most rows of sales, with how many rows each has,
    most first: the category names are the index and the counts are the values.

    For a category column holding produce, honey, produce, crafts, produce and honey, top_categories(sales, 2)
    gives produce 3 and honey 2.
    """
    raise NotImplementedError("Not built yet.")

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

  1. load_sales(path) reads the CSV file at path. On the seeded file: 1,900 rows and 6 columns, with 12 units missing.
  2. load_vendors(path, year) reads the sheet named for year from the workbook at path, stepping over the two title rows, so the columns are vendor_id, vendor_name, stall, category and joined. load_vendors(path, 2026) gives 15 rows and 5 columns, starting with V01, Green Acre Greens. The year may come as the number 2026 or the text '2026'; both mean the sheet named 2026.
  3. load_reviews(path) reads the reviews JSON at path and returns one row for each review, with the columns stars, comment, date and vendor_id. On the seeded file: 104 rows. A vendor with no reviews has no rows.
  4. table_summary(df) returns a dictionary with four keys: rows, columns, numeric (a list of the number columns' names) and missing (each column's name and how many of its values are missing). For a table with product and units, 3 rows and one unit missing, it gives {'rows': 3, 'columns': 2, 'numeric': ['units'], 'missing': {'product': 0, 'units': 1}}.
  5. top_categories(sales, n) returns the n categories with the most rows and how many each has, most first. For produce, honey, produce, crafts, produce and honey, top_categories(sales, 2) gives produce 3 and honey 2.

table_summary and top_categories only read the table they're given; the tests check it's unchanged.

Check your work

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

python -m pytest

At first all 19 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 load_vendors(path, 2026).shape should give (15, 5), but it gave (17, 5).

To see your functions at work on the whole market, run python report.py.

Hints

  • Look before you write. Run the notebook's cells: head(), info() and the workbook's column names show you what each file holds, as A First Look: head, info and describe does.
  • Sheet names are text. A number in sheet_name means a tab's position, not its name; Loading an Excel Workbook shows the difference, and str(year) turns a number into text.
  • Columns named Unnamed: 1 mean title rows. Count how many rows sit above the real header, and step over them with skiprows.
  • json_normalize takes Python data. Read the file with json.load first, then name the list with record_path and the vendor's field with meta, as Loading JSON, Flat and Nested shows.
  • Two tools do most of table_summary. describe() sums up only the number columns, so its columns are the number columns; len(df) - df.count() counts each column's missing values, and to_dict() turns that Series into a dictionary.
  • top_categories is one line. Counting What's There: value_counts and unique has everything it needs.

When you're done

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

1) sales rows: 1900
2) sales columns: 6
3) number columns: units, price
4) units missing: 12
5) top categories: produce (654), baked goods (415), crafts (399)
6) vendors in 2026: 15
7) vendors in 2025: 15
8) reviews: 104
9) vendors with reviews: 14
10) average stars: 4.09

Now look at line 4 and line 9: 12 units are missing, and only 14 of the 15 vendors have reviews. Before anyone averages units or stars for the meeting, what would you tell the coordinator about each?

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-farmers-market-first-look), 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-farmers-market-first-look.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-farmers-market-first-look, 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

Every analysis starts with loading the data and saying what's in it, and as tested functions those first steps give the same answer every time, whichever format the next file arrives in.

Key terms

Related