Code of the Day
BeginnerData Wrangling

Lab: Wrangle some data

Read a CSV of product inventory, filter low-stock items, and write the results to JSON — the complete parse-transform-serialise loop.

Lab · optionalWorkflowBeginner25 min
By the end of this lesson you will be able to:
  • Parse a CSV string with csv.DictReader into a list of dicts
  • Filter rows based on a numeric field
  • Transform records by adding a computed field
  • Serialise filtered results to a formatted JSON string

You have a CSV export of product inventory. Your job: find items that are running low on stock, add a computed urgency field, and write the results to JSON so a downstream system can process them.

This lab exercises the complete data-wrangling loop: parse → filter → transform → serialise.

The data

sku,name,category,stock,reorder_threshold
A001,Wireless Keyboard,peripherals,14,20
A002,USB-C Hub,peripherals,5,15
A003,Monitor Stand,accessories,32,10
A004,Webcam HD,peripherals,2,10
A005,Desk Lamp,accessories,0,5
A006,Laptop Sleeve,accessories,18,25
A007,Mechanical Keyboard,peripherals,7,20
A008,Mouse Pad XL,accessories,11,10

A product needs reordering when stock < reorder_threshold. The urgency is reorder_threshold - stock — how many units short you are.

Checkpoint 1 — parse the CSV

Read every row into a list of dicts and print the first two:

Python — editable, runs in your browser

Checkpoint 2 — filter and convert types

Find products that need reordering. Remember: every value from CSV is a string, so convert stock and reorder_threshold to integers before comparing:

Python — editable, runs in your browser

Checkpoint 3 — transform and add a computed field

Build a clean output record for each low-stock item. Add the computed urgency field and drop the raw CSV strings in favour of typed values:

Python — editable, runs in your browser

Checkpoint 4 — serialise to JSON

Write the final output as formatted JSON:

Python — editable, runs in your browser

The structure mirrors the automation lab: separate functions for parsing, processing, and output. If the CSV later comes from a real file, only parse_inventory() changes — the filtering and serialisation logic stays intact.

Extend it yourself

  1. Group the reorder list by category in the JSON output instead of a flat list.
  2. Add a "generated_at" timestamp field to the output using datetime.datetime.now().isoformat().
  3. Change parse_inventory() to accept a file path and read from a real CSV on disk.
Finished reading? Mark it complete to track your progress.

On this page