BuildlyBuildly
DashboardExercisesProfileHelp
Back to all

Practice: Pivot & Reshape

Drills tagged with this skill, unpassed first. Pass them all to mark the skill ready for scenarios.

Module 3: Importing & Handling Files

0/3 done
Exercises (0/3)
  • 1
    Summarize with a Pivot Table
    Context. A flat table of sales rows — each tagged with a region and a quarter — needs to become a region-by-quarter summary grid. Your task. Write sales_pivot(df) that returns a pivot table with one row per region, one column per quarter, and the sum of amount in each cell. Turn the region index back into a regular column. Example. rows for East/Q1 and East/Q2 collapse to a single East row with a Q1 and a Q2 column. Notes. Several rows can share the same region+quarter — sum them. Every region/quarter combination in the data has at least one row.
    7 min· 10 XP
  • 2
    Reshape Wide to Long
    Context. A scores table is wide — one column per subject. For analysis you need it long: one row per student-subject pair. Your task. Write to_long(df) that melts the wide table into columns student, subject, and score. Example. | student | math | science | |---------|------|---------| | Alice | 90 | 85 | becomes rows (Alice, math, 90) and (Alice, science, 85). Notes. student stays as an identifier column. Every other column becomes a subject value.
    7 min· 10 XP
  • 3
    Reshape Long to Wide
    Context. A scores table is long — one row per student-subject pair. For a report card you need it wide: one row per student, one column per subject. Your task. Write to_wide(df) that pivots the long table so each student is a row and each subject is its own column holding the score. Turn the student index back into a regular column. Example. rows (Alice, math, 90) and (Alice, science, 85) collapse to one Alice row with math and science columns. Notes. Each student-subject pair appears exactly once. Every student has a score for every subject in the data.
    7 min· 10 XP