Reshape Wide Data to Long

easy
Unit 4 · Pandas I0/3 to milestone

Spreadsheets often store one column per metric per year: sales_2022, sales_2023, cost_2022, … That's awkward to analyse. Tidy data has one row per observation.

Implement to_long(df, id_col): turn every column named <metric>_<year> into rows with columns id_col, year (int) and one column per metric. Sort by id_col, then year, with a fresh index. Metric columns should be ordered alphabetically after year. A missing combination becomes NaN.

Then implement to_wide(long_df, id_col), the inverse, with columns id_col followed by <metric>_<year> sorted by metric then year.

#pandas#reshaping#tidy-data2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Reshape Wide Data to Long · TechGradify