stack() and unstack() in Pandas - Time & Space Complexity
We want to understand how the time needed to reshape data with stack() and unstack() grows as the data size increases.
How does the work change when the data has more rows or columns?
Analyze the time complexity of the following code snippet.
import pandas as pd
# Create a DataFrame with 1000 rows and 5 columns
df = pd.DataFrame({f'col{i}': range(1000) for i in range(5)})
# Use stack to reshape columns into rows
stacked = df.stack()
# Use unstack to reshape back
unstacked = stacked.unstack()
This code reshapes a DataFrame by stacking columns into a longer format and then unstacks it back to the original shape.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing all elements in the DataFrame to rearrange them.
- How many times: Each element is visited once during
stack()and once duringunstack().
As the number of rows and columns grows, the total elements grow by multiplying rows and columns.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 5 = 50 | About 50 operations per stack or unstack |
| 100 x 5 = 500 | About 500 operations per stack or unstack |
| 1000 x 5 = 5000 | About 5000 operations per stack or unstack |
Pattern observation: The work grows roughly in direct proportion to the total number of elements.
Time Complexity: O(n x m)
This means the time needed grows linearly with the total number of elements, where n is rows and m is columns.
[X] Wrong: "Stacking or unstacking only touches a few elements, so it's very fast regardless of size."
[OK] Correct: Actually, these operations must visit every element to rearrange them, so the time grows with the total data size.
Knowing how reshaping data scales helps you explain your choices clearly and shows you understand the cost of data transformations.
"What if we stacked a DataFrame with a MultiIndex on rows? How would the time complexity change?"
Practice
stack() function do in pandas?Solution
Step 1: Understand the purpose of
stack()stack()takes the columns of a DataFrame and stacks them into the row index, making the data longer.Step 2: Compare with other options
unstack()does the opposite by moving rows into columns. Sorting and filtering are unrelated tostack().Final Answer:
It moves columns into rows, making the data longer. -> Option CQuick Check:
stack()= columns to rows [OK]
- Confusing stack() with unstack()
- Thinking stack() sorts data
- Assuming stack() filters rows
df?Solution
Step 1: Recall the method names
The method to move rows into columns isunstack(), sodf.unstack()is correct.Step 2: Check other options
df.stack()stacks columns into rows.unstacked()does not exist.stack(unstack=True)is invalid syntax.Final Answer:
df.unstack() -> Option BQuick Check:
Unstack method isunstack()[OK]
- Using stack() instead of unstack()
- Calling a non-existent method unstacked()
- Passing wrong arguments to stack()
import pandas as pd
df = pd.DataFrame({
'A': [1, 2],
'B': [3, 4]
}, index=['x', 'y'])
stacked = df.stack()What is the output of
stacked?Solution
Step 1: Understand stacking on df
Stack moves columns into rows, creating a Series with a MultiIndex combining the original index and column labels.Step 2: Check the resulting Series
The index pairs are ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4, matching A Series with MultiIndex: ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4.Final Answer:
A Series with MultiIndex: ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4 -> Option DQuick Check:
stack() output is MultiIndex Series [OK]
- Expecting a DataFrame output instead of Series
- Confusing index and columns after stacking
- Ignoring MultiIndex structure
df = pd.DataFrame({
'X': [10, 20],
'Y': [30, 40]
}, index=['a', 'b'])
result = df.unstack(level=2)What error will occur?
Solution
Step 1: Check the index levels of df
df has a single index level ('a', 'b'), so level 2 does not exist.Step 2: Understand unstack(level=2) behavior
Requesting level 2 causes a KeyError because that level is missing.Final Answer:
KeyError: Level 2 not found -> Option AQuick Check:
unstack(level) must match existing index levels [OK]
- Assuming unstack() accepts any level number
- Expecting no error on invalid level
- Confusing level number with label
import pandas as pd
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1)])
df = pd.DataFrame({'val': [10, 20, 30]}, index=index)
How can you reshape
df so that the second level of the index becomes columns?Solution
Step 1: Identify the MultiIndex levels
The DataFrame has two index levels: level 0 ('A', 'B') and level 1 (1, 2).Step 2: Use unstack(level=1) to move second level to columns
Unstacking level 1 moves that index level into columns, reshaping the data as required.Final Answer:
Use df.unstack(level=1) to move second index level to columns. -> Option AQuick Check:
unstack(level=1) moves second index level to columns [OK]
- Using stack() instead of unstack()
- Unstacking wrong level number
- Ignoring MultiIndex structure
Ready to prove it?
Pass the test and this pattern moves unknown
