pandas in depth · lesson 12 of 12
Parts in this lesson
Amerge(..., validate="many_to_one")4.3: a join that can’t double rows
Bpivot_table(index=..., columns=..., aggfunc="sum")4.4: a summary table
Cresample("W").sum().rolling(4).mean()4.7: weeks, smoothed

Project: the Tindahan report tables

Last updated

The Tindahan shops now have two months of clean data in four tables: sales, stores, products and weather. In this project you'll join them safely into one table, then build the four summary tables a monthly report needs: revenue by store and month, the best seller in each town, weekly rice sales with a rolling average, and sales on rainy days against dry ones.

By the end of this lesson you can

  • Join four tables with merge, checked with validate
  • Build summary tables with pivot_table, groupby and resample
  • Compare months fairly when they have different numbers of days
  • Write down what each table shows, and what it doesn’t

Plan it

The shops' owner wants a short report each month. Before any chart (that's Stage 6), the report needs its numbers, as four tables:

  1. Revenue by store and month, in pesos.
  2. The best seller in each town, by kilos.
  3. Weekly rice sales for all stores, with a 4-week rolling average to show the trend.
  4. Rain against sales: do the stores sell less rice on rainy days?

The data is in four clean files in data/clean/, for January and February 2024. The data is made up for this course, simulated to behave like a real shop's, so the numbers are about the method, not about real stores.

fileone row percolumns
sales.csvstore, day and itemdate, store, item, kilos
stores.csvstorestore, town
products.csvitemitem, price_per_kilo
weather.csvtown and daydate, town, rain_mm

The plan: join everything onto the sales rows, so each sale knows its town, price and rain. Then each report table is a few lines on that one table. You'll make 03-report.ipynb next to the first two notebooks.

Join with validate

Run the cells.

03-report.ipynb
  1. Tindahan: report tables, January and February 2024

    Joins the four clean tables and builds the summary tables for the monthly report.

720 sales rows, 4 stores, 3 products and 240 days of weather (60 days in 4 towns). After three joins there are still 720 rows, and no missing values anywhere.

Those two numbers are the checks that matter:

  • validate="many_to_one" on every join says that each sale matches at most one store, one product and one day's weather. If stores.csv ever had a store twice, the join would stop with a MergeError instead of quietly doubling that store's sales (lesson 4.3).
  • how="left" keeps every sale even if it has no match, and the NaN count shows whether any didn't match. Zero means every store had a town, every item a price and every town-day its weather.

The weather is joined on two keys, date and town, because rain on a day differs from town to town.

The report tables

Here's the whole report notebook: the join in one cell, then a note and a cell for each table. Run all.

03-report.ipynb
  1. Revenue by store and month, in pesos.

  2. Best seller in each town, by kilos.

  3. Weekly rice sales, all stores, with a 4-week rolling average.

  4. Rain against sales. Rice kilos per store-day, on dry and rainy days.

  5. What the tables show

    -

Revenue by store and month. Batangas earned the most both months, ₱99,451 in January. Every store earned less in February, but February 2024 had 29 days and January 31. Per day, Lipa (₱2,290 to ₱2,383) and Rosario (₱1,545 to ₱1,614) actually did better in February; Batangas and Tanauan did slightly worse. A report that says "sales fell in February" would be wrong for two of the four stores. Compare per day, or say clearly that the months differ in length.

Best seller in each town. Rice, in all four towns, by a long way. Sorting by kilos and keeping the first row per town with drop_duplicates("town") is a short way to take the top item of each group. By kilos, though, not by pesos: oil sells fewer kilos at a higher price, so "best seller" needs its measure written next to it.

Weekly rice sales. About 900 to 960 kg a week in the first six weeks, with the 4-week average starting at 931.2 kg in the fourth week. The first three weeks have no rolling value (NaN). 1 January 2024 was a Monday, so the first week is a full one, but 29 February was a Thursday, so the last "week" has only four days and will look like a drop. Say so, or leave it out.

Rain against sales. On dry store-days, a store sold 34.3 kg of rice on average, over 166 days; on rainy ones, 29.8 kg, over 74. That fits the idea that rain keeps shoppers home. It isn't proof: rainy days might also be weekdays, or one town might be both rainier and smaller. Stage 8 tests whether a difference like this could be chance, and Stage 9 looks at what else might explain it. For now, report the difference, the counts, and the warning.

Last, the note at the bottom, What the tables show. Click Edit note and write one line per table, in plain words, with its caution. For example:

  • Rice is the best seller in every town by kilos; by revenue the order may differ.

Finished

Press Restart and run all on the notebook in step 3. It should show 720 rows after the joins, and four tables.

Look at what you used:

  • merge with validate and how="left" (4.3), and two keys at once.
  • assign with a lambda, in a method chain (4.10).
  • pivot_table (4.4) and .dt.strftime (4.6) for months as columns.
  • groupby with as_index=False and agg (4.1 and 4.5).
  • resample and rolling (4.7) for weeks and a trend.

The project folder now has a raw file, a first-look notebook, a cleaning notebook and a report notebook. In Stage 5 you'll do what this project skipped: get data from spreadsheets, databases and the web, and write a cleaning pipeline that produces these four clean files from raw ones every time.

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis