Stack and unstack help you reshape data tables easily. They let you switch between wide and long formats to see data differently.
Stack and unstack in 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.
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)
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)
import pandas as pd df_empty = pd.DataFrame() print('Stacking empty DataFrame:') print(df_empty.stack())
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)
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.
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)
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).
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.
Practice
stack() method do to a DataFrame in pandas?Solution
Step 1: Understand the stack operation
Thestack()method compresses columns into a single column, turning columns into rows.Step 2: Compare with unstack
Unlikeunstack(), which moves rows to columns,stack()makes data longer by stacking columns.Final Answer:
It moves columns into rows, making the data longer. -> Option AQuick Check:
Stack = columns to rows [OK]
- Confusing stack with unstack direction
- Thinking stack sorts data
- Assuming stack removes duplicates
df?Solution
Step 1: Recall unstack syntax
Theunstack()method by default unstacks the last level of the index, andlevel=-1explicitly specifies this.Step 2: Differentiate from stack
stack()is the opposite operation, so options A and B are incorrect.unstack()does not take anaxisparameter, so df.unstack(axis=1) is invalid.Final Answer:
df.unstack(level=-1) -> Option CQuick Check:
Unstack last level = df.unstack(level=-1) [OK]
- Using stack instead of unstack
- Passing axis parameter to unstack
- Not specifying level when needed
df below:import pandas as pd
d = {('A', 'x'): 1, ('A', 'y'): 2, ('B', 'x'): 3, ('B', 'y'): 4}
df = pd.Series(d).unstack()What is the output of
df.stack()?Solution
Step 1: Understand the initial DataFrame
Theunstack()converts the Series with MultiIndex into a DataFrame with columns 'x' and 'y' and index 'A' and 'B'.Step 2: Apply
Callingstack()on the DataFramestack()moves the columns 'x' and 'y' back into the inner level of the row index, producing a Series with MultiIndex.Final Answer:
A Series with MultiIndex where second level is stacked into rows -> Option BQuick Check:
Stack reverses unstack, making columns into row index [OK]
- Expecting a DataFrame output instead of Series
- Confusing which index level is stacked
- Thinking stack causes error here
df = pd.DataFrame({
'A': [1, 2],
'B': [3, 4]
})
stacked = df.stack(level=1)What is the cause of the error?
Solution
Step 1: Check DataFrame columns
The DataFrame has simple columns 'A' and 'B', which are not a MultiIndex, so specifyinglevel=1is invalid.Step 2: Understand stack level parameter
Thelevelparameter instack()refers to the level of the columns MultiIndex to stack. Without MultiIndex columns, level=1 causes an error.Final Answer:
DataFrame columns have no MultiIndex, so level=1 is invalid. -> Option DQuick Check:
Stack level requires MultiIndex columns [OK]
- Assuming stack only works on Series
- Thinking stack requires numeric data
- Ignoring the column index type
df with a MultiIndex on rows (levels: 'City', 'Year') and columns as years (e.g., 2020, 2021). You want to reshape it so that the 'Year' level from rows becomes columns, and the columns years become a new row level. Which sequence of operations correctly achieves this?Solution
Step 1: Unstack the 'Year' level from rows
Unstacking the 'Year' level moves it from the row index to columns, widening the DataFrame.Step 2: Stack the columns
Stacking the columns moves the columns years into a new row index level, making the data longer again but reshaped as desired.Final Answer:
First unstack the 'Year' level from rows, then stack the columns. -> Option AQuick Check:
Unstack rows then stack columns reshapes MultiIndex correctly [OK]
- Stacking before unstacking causes wrong shape
- Unstacking wrong index level
- Ignoring the need to reshape both rows and columns
Ready to prove it?
Pass the test and this pattern moves unknown
