Data Science · Intermediate level

Intermediate level · Module 2: Data Wrangling with pandas and NumPy · Lesson 1 of 3

Tidy Data and Reshaping

Organise data so each variable is a column and each observation a row, and convert between wide and long formats.

In this lesson you will

  • Explain tidy data principles
  • Reshape with melt and pivot_table

Key concepts

tidy datawide vs long formatmeltpivot_table

The lesson

Tidy data has one variable per column, one observation per row and one value per cell. Most analysis and plotting tools expect it.

Reports often come wide: one column per month. melt turns them long — school, month, attendance — which makes grouping and charting by month easy.

pivot_table does the reverse, for example to present a table of schools by month, and can aggregate while it reshapes.

Step by step

  1. Identify untidy features in the attendance file.
  2. Melt monthly columns into long format.
  3. Summarise attendance by month.
  4. Pivot back into a presentation table.

Practice activity

Convert the attendance records into tidy format and produce a monthly summary table.

Expected result: Tidy data and a correct monthly table.

Check your understanding

  1. What does melt do?

    Show answer

    Turns wide columns into long rows.

  2. Name one principle of tidy data.

    Show answer

    Each variable has its own column (or each observation its own row).

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.