Course contentsShow
← Python
Lesson 950 of 1,06015. Data Science FundamentalsFree lesson

Reshaping Data with Stack and Unstack

Transform wide-format datasets into long-format structures and vice versa to facilitate easier analysis and visualization of multi-level indexed data.

In this lesson, you will learn how to pivot between wide and long data formats using the stack and unstack methods to make multi-level indexed data easier to analyze.

The Core Concept

Data often arrives in "wide" format, where variables are spread across columns, or "long" format, where variables are stacked into a single column identified by an index. Pandas provides stack() and unstack() to fluidly move between these shapes, specifically when your DataFrame uses Hierarchical Indexing.

  • stack(): This "pivots" the innermost column level into the row index. It effectively makes your DataFrame "longer" by compressing columns into rows.
  • unstack(): This is the inverse. It pivots the innermost row index level back into the column headers, making your DataFrame "wider."

Practical Illustration

Imagine a DataFrame tracking Temperature and Humidity across two cities, NYC and LA, indexed by Date.

import pandas as pd

# Creating a MultiIndex DataFrame
index = pd.MultiIndex.from_tuples([('2023-01-01', 'NYC'), ('2023-01-01', 'LA')])
df = pd.DataFrame({'Temp': [32, 60], 'Humidity': [40, 50]}, index=index)

# Unstacking the City level to make it wide
wide_df = df.unstack()

# Stacking back to long format
long_df = wide_df.stack()

When you call unstack(), the City level moves from the row index to the columns. If you had a multi-level index on your rows (like Date and City), unstack(level=1) would specifically target the City level to move it, allowing you to choose exactly which dimension to flatten.

Why It Matters

This is essential for visualization and statistical modeling. Many libraries (like seaborn) expect data in "long" format, where each row represents a single observation and columns represent attributes. Conversely, "wide" format is often more intuitive for human reading or performing cross-column arithmetic. Mastering these tools allows you to restructure your data to match the requirements of your analysis pipeline without needing to re-import or clean the dataset from scratch.

Key Takeaway: Use stack() to pivot columns into rows for a long, analysis-ready format, and unstack() to pivot rows into columns for a wide, comparison-friendly structure.