wide is a sales extract stitched together from several source systems. It has a region column and one column per month, named "YYYY-MM". It's messy:
- •the month columns can be in any order, and a month with no data in any system is missing from the columns entirely;
- •a region can appear on several rows (one per source system);
- •empty cells are
NaN.
Write consolidate(wide) returning a clean wide table:
- •one row per region, regions sorted alphabetically, with
region as a regular column (not the index); - •one column per month from the earliest to the latest month in the input columns, every month in between included, in calendar order;
- •each cell is the region's total for the month across all its rows, with
NaNs and missing months counted as 0.
Columns: region, then the month columns.
Python 3.13 in your browser — the standard library plus pandas and numpy; no pip installs.