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
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
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
wide = df.pivot_table(index="date", columns="city", values="sales", aggfunc="sum")
print(wide)melt: wide → long
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()orwide.columnsrenaming if needed.
Visualize it
flowchart LR W["Wide format
(one column per city)"] -->|"melt()"| L["Long format
(one row per observation)"] L -->|"pivot() / pivot_table()"| W
🧪 Try It Yourself
Exercise 1 – Melt Wide to Long
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Pivot Long Back to Wide
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Aggregate Duplicates With pivot_table
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
What happens when the index and columns combination is not unique in df.pivot(...)?
pivot needs unique pairs. When duplicates are possible use pivot_table, which aggregates them with an aggfunc.
Show answer
A — pivot raises an error — pivot needs unique pairs. When duplicates are possible use pivot_table, which aggregates them with an aggfunc.
Which function turns a wide table with pune and delhi columns into one row per date and city?
melt goes wide to long, using id_vars to keep date, var_name for the city column and value_name for the values.
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.
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?
Each of the 2 dates is paired with each of the 2 city columns, giving 4 long rows.
Show answer
B — 4 — Each of the 2 dates is paired with each of the 2 city columns, giving 4 long rows.
Which format do many ML and EDA workflows prefer, according to the lesson?
Long format is better for analysis and plotting, while wide format is good for reporting.
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.