Reshaping Data
Transform data between wide and long formats using melt(), stack(), and unstack().
Wide to Long with melt()
Convert columns into rows:
Loading Pandas Playground...
Long to Wide with pivot()
Convert rows into columns:
Loading Pandas Playground...
Stack and Unstack
Work with multi-level indices:
Loading Pandas Playground...
Melt Multiple Value Columns
Unpivot several columns at once:
Loading Pandas Playground...
Combining Melt with GroupBy
Reshape then aggregate:
Loading Pandas Playground...
Transpose
Swap rows and columns:
Loading Pandas Playground...
Exercise: Wide to Long
Loading Exercise...
Exercise: Long to Wide
Loading Exercise...
Key Points
melt()converts wide to long (unpivot)pivot()converts long to widestack()pivots columns to rowsunstack()pivots rows to columns.Ttransposes rows and columns- Long format is often better for analysis
- Wide format is often better for display

