Pivot to wide, unpivot to long, and check the total survives
Task
Count orders by region and month, pivot the result into a wide table with a column per month, unpivot it back to long, and prove the reshaping moved the numbers around without changing them.
Steps
- Join orders to customers in
data/shop.dband count orders by region and month. Save the long result tolab/reshape/long.csvwith headerregion,month,orders. - Pivot it to
lab/reshape/wide.csvwith one row per region and one column per month, 2025-01 to 2025-12. - Unpivot the wide table back to
lab/reshape/back.csvwith the long header. - Record the grand total of each file in
lab/reshape/totals.txtaslong: N,wide: Nandback: N.
Verify
These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.
awk -F, 'NR>1 {n++} END {print n" long rows"}' lab/reshape/long.csv
awk -F, 'NR>1 {n++} END {print n" wide rows"}' lab/reshape/wide.csv
awk -F, 'NR>1 {n++} END {print n" rows after unpivoting"}' lab/reshape/back.csv
awk -F': ' '{print $2}' lab/reshape/totals.txt | sort -u
48 long rows, 4 wide rows with twelve month columns, and 48 again after unpivoting -- and a single total, 2000, for all three files. The row count changes with every reshape; the total must not.
This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.