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

Restructuring Data with Stack and Unstack

Transform data between wide and long formats by pivoting index levels into columns to prepare for specific analytical requirements.

In this lesson, you will learn how to shift your data between "wide" and "long" formats using the stack and unstack methods to suit your analytical needs.

From Wide to Long: The stack Method

Often, your data comes in a "wide" format where column headers represent different categories or time points. If you need to perform calculations across these columns or prepare the data for visualization tools that prefer tidy (long) formats, you use stack().

When you call stack(), it takes the columns of your DataFrame and "pivots" them into a new inner level of the index. Essentially, you are taking the column headers and turning them into rows.

From Long to Wide: The unstack Method

Conversely, unstack() is the inverse operation. If your data is currently in a hierarchical index (MultiIndex) and you want to move one of those index levels back into the columns, unstack() is the tool for the job. This is particularly useful when you have a MultiIndex representing, for example, "Date" and "Category," and you want to treat each "Category" as its own individual column for easier comparison.

Concrete Example

Consider a DataFrame df where index levels are [Year, Quarter] and columns are [Sales, Profit]:

import pandas as pd

# If we want to turn the columns into an index level:
long_df = df.stack() 
# Result: The columns 'Sales' and 'Profit' are now part of the index.

# If we want to take the 'Quarter' index level and move it to columns:
wide_df = df.unstack(level='Quarter')
# Result: Each Quarter now becomes a column header.

By moving levels between the axes, you change the granularity of your data. Using stack creates a taller, thinner DataFrame, while unstack creates a shorter, wider one. This flexibility allows you to align your data structure with the specific requirements of the library or statistical test you are using.

Key Takeaway: Use stack to rotate columns into the index (long format) and unstack to rotate index levels into the columns (wide format).