Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Reshaping Data (pivot, pivot_table, melt)

Convert between wide and long formats using pivot/pivot_table and melt.

Long vs wide (why reshaping exists)

  • Long format: one row per observation (better for analysis/plotting)
  • Wide format: values spread across columns (good for reporting)

Example: long format

Python
import pandas as pd
 
df = pd.DataFrame({
    "date": ["2025-01-01", "2025-01-01", "2025-01-02", "2025-01-02"],
    "city": ["pune", "delhi", "pune", "delhi"],
    "sales": [100, 120, 90, 150],
})
 
print(df)

pivot: reshape when data is unique

Python
wide = df.pivot(index="date", columns="city", values="sales")
print(wide)

If the combination of (index, columns) isn't unique, pivot throws an error.

pivot_table: reshape with aggregation

Python
wide = df.pivot_table(index="date", columns="city", values="sales", aggfunc="sum")
print(wide)

melt: wide → long

Python
wide = pd.DataFrame({
    "date": ["2025-01-01", "2025-01-02"],
    "pune": [100, 90],
    "delhi": [120, 150],
})
 
long = wide.melt(id_vars=["date"], var_name="city", value_name="sales")
print(long)

Practical notes

  • Many ML/EDA workflows prefer long format.
  • Pivoted tables often have a multi-index for columns—use reset_index() or wide.columns renaming if needed.

Visualize it

diagram Wide vs long, and how to move between them mermaid
melt turns wide columns into long rows; pivot/pivot_table turns long rows back into wide columns.
sketch Wide table <-> long table p5.js
melt stacks columns into rows; pivot spreads rows back out into columns. Same data, two shapes.

🧪 Try It Yourself

Exercise 1 – Melt Wide to Long

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Pivot Long Back to Wide

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Aggregate Duplicates With pivot_table

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What happens when the index and columns combination is not unique in df.pivot(...)?

    Show answer

    A — pivot raises an error — pivot needs unique pairs. When duplicates are possible use pivot_table, which aggregates them with an aggfunc.

  2. Which function turns a wide table with pune and delhi columns into one row per date and city?

    Show answer

    A — melt — melt goes wide to long, using id_vars to keep date, var_name for the city column and value_name for the values.

  3. A wide DataFrame has 2 dates and columns pune and delhi. How many rows does wide.melt(id_vars=["date"], var_name="city", value_name="sales") return?

    Show answer

    B — 4 — Each of the 2 dates is paired with each of the 2 city columns, giving 4 long rows.

  4. Which format do many ML and EDA workflows prefer, according to the lesson?

    Show answer

    A — Long format, one row per observation — Long format is better for analysis and plotting, while wide format is good for reporting.

Next

Reshaping assumes your tables are already combined — continue to Merging and Joining Data (merge, join, concat) for how to bring multiple tables together in the first place.

Was this page helpful?

Let us know how we did