0
0
PandasComparisonBeginner · 3 min read

Stack vs Unstack pandas: Key Differences and When to Use Each

In pandas, stack() converts columns into rows, creating a longer DataFrame by pivoting columns into the index. Conversely, unstack() pivots rows into columns, making the DataFrame wider by moving an index level into columns.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of stack() and unstack() in pandas.

Aspectstack()unstack()
PurposeMoves columns to rows (compresses columns)Moves rows to columns (expands columns)
InputDataFrame with columns to stackDataFrame with multi-level index to unstack
OutputSeries or DataFrame with longer shapeDataFrame with wider shape
Typical use caseConvert wide data to long formatConvert long data to wide format
Index/Column effectColumns become inner index levelInner index level becomes columns
Common axis affectedColumns axis (axis=1)Index axis (axis=0)
⚖️

Key Differences

stack() and unstack() are inverse operations in pandas used to reshape data. stack() compresses the columns of a DataFrame into the inner level of the row index, effectively turning columns into rows. This is useful when you want to convert a wide DataFrame into a longer, more compact format.

On the other hand, unstack() pivots the inner level of the row index back into columns, expanding the DataFrame horizontally. It is commonly used to convert a long DataFrame back into a wide format. Both methods work best with DataFrames that have a MultiIndex either on columns (stack()) or on rows (unstack()).

While stack() reduces the number of columns by moving them into the index, unstack() increases the number of columns by moving an index level into columns. Understanding which axis to reshape and the structure of your DataFrame's index and columns is key to using these methods effectively.

⚖️

Code Comparison

Example showing how stack() converts columns into rows.

python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data, index=['x', 'y'])
stacked = df.stack()
print(stacked)
Output
x A 1 B 3 y A 2 B 4 dtype: int64
↔️

Unstack Equivalent

Using unstack() to reverse the stacking and convert rows back into columns.

python
unstacked = stacked.unstack()
print(unstacked)
Output
A B x 1 3 y 2 4
🎯

When to Use Which

Choose stack() when you want to transform a wide DataFrame into a longer format by turning columns into rows, which is helpful for data analysis or plotting that requires long-form data.

Choose unstack() when you need to pivot a DataFrame from a long format back to a wide format by moving an index level into columns, often useful for creating summary tables or reports.

In short, use stack() to compress columns into rows and unstack() to expand rows into columns.

Key Takeaways

stack() moves columns into the row index, making data longer.
unstack() moves an index level into columns, making data wider.
They are inverse operations useful for reshaping DataFrames.
Use stack() to go from wide to long format.
Use unstack() to go from long to wide format.