0
0
Data Analysis Pythondata~5 mins

Stack and unstack in Data Analysis Python

Choose your learning style9 modes available
Introduction

Stack and unstack help you reshape data tables easily. They let you switch between wide and long formats to see data differently.

You want to turn columns into rows to analyze data better.
You have grouped data in rows and want to spread it into columns.
You want to prepare data for charts that need a specific shape.
You need to clean data by moving hierarchical indexes between rows and columns.
Syntax
Data Analysis Python
import pandas as pd

# Stack moves columns to rows
stacked_df = df.stack()

# Unstack moves rows to columns
unstacked_df = df.unstack()

Stack works on columns and turns them into a new inner row index.

Unstack works on row indexes and turns one level into columns.

Examples
Stacking a simple DataFrame moves columns A and B into rows under a new inner index.
Data Analysis Python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)

print('Original DataFrame:')
print(df)

stacked = df.stack()
print('\nStacked DataFrame:')
print(stacked)
Unstacking a MultiIndex DataFrame moves the 'second' level of the row index into columns.
Data Analysis Python
import pandas as pd

# DataFrame with MultiIndex rows
arrays = [['bar', 'bar', 'baz', 'baz'], ['one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}
df = pd.DataFrame(data, index=index)

print('Original DataFrame with MultiIndex:')
print(df)

unstacked = df.unstack(level='second')
print('\nUnstacked DataFrame:')
print(unstacked)
Stacking an empty DataFrame returns an empty Series without error.
Data Analysis Python
import pandas as pd

df_empty = pd.DataFrame()
print('Stacking empty DataFrame:')
print(df_empty.stack())
Stacking a DataFrame with one row and one column creates a Series with one element.
Data Analysis Python
import pandas as pd

df_single = pd.DataFrame({'A': [10]})
print('Original single-element DataFrame:')
print(df_single)

stacked_single = df_single.stack()
print('\nStacked single-element DataFrame:')
print(stacked_single)
Sample Program

This program creates a DataFrame with two levels in rows and columns. It stacks the first level of columns into rows, then unstacks the second level of rows back into columns. This shows how stacking and unstacking reshape data.

Data Analysis Python
import pandas as pd

# Create a DataFrame with MultiIndex rows and columns
arrays_rows = [['group1', 'group1', 'group2', 'group2'], ['A', 'B', 'A', 'B']]
index = pd.MultiIndex.from_arrays(arrays_rows, names=['group', 'subgroup'])

arrays_cols = [['score', 'score', 'rank', 'rank'], ['math', 'english', 'math', 'english']]
columns = pd.MultiIndex.from_arrays(arrays_cols)

data = [[90, 80, 1, 2], [85, 88, 2, 1], [78, 75, 3, 4], [82, 79, 4, 3]]
df = pd.DataFrame(data, index=index, columns=columns)

print('Original DataFrame:')
print(df)

# Stack the columns to rows (move inner column level to rows)
stacked_df = df.stack(level=0)
print('\nDataFrame after stacking columns (level=0):')
print(stacked_df)

# Unstack the rows to columns (move inner row level to columns)
unstacked_df = stacked_df.unstack(level='subgroup')
print('\nDataFrame after unstacking rows (level="subgroup"):')
print(unstacked_df)
OutputSuccess
Important Notes

Stack and unstack have time complexity roughly O(n), where n is the number of elements in the DataFrame.

They use extra memory to create reshaped copies, so space complexity is also O(n).

A common mistake is forgetting which index level to stack or unstack, causing confusing results.

Use stack when you want to make data longer (more rows), and unstack when you want to make data wider (more columns).

Summary

Stack moves columns into rows, making data longer.

Unstack moves rows into columns, making data wider.

They help reshape data for easier analysis and visualization.