Stack vs Unstack pandas: Key Differences and When to Use Each
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.
| Aspect | stack() | unstack() |
|---|---|---|
| Purpose | Moves columns to rows (compresses columns) | Moves rows to columns (expands columns) |
| Input | DataFrame with columns to stack | DataFrame with multi-level index to unstack |
| Output | Series or DataFrame with longer shape | DataFrame with wider shape |
| Typical use case | Convert wide data to long format | Convert long data to wide format |
| Index/Column effect | Columns become inner index level | Inner index level becomes columns |
| Common axis affected | Columns 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.
import pandas as pd data = {'A': [1, 2], 'B': [3, 4]} df = pd.DataFrame(data, index=['x', 'y']) stacked = df.stack() print(stacked)
Unstack Equivalent
Using unstack() to reverse the stacking and convert rows back into columns.
unstacked = stacked.unstack()
print(unstacked)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.stack() to go from wide to long format.unstack() to go from long to wide format.