Challenge - 5 Problems
Stack and Unstack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of stacking a DataFrame with MultiIndex columns?
Given the following DataFrame, what will be the result of
df.stack()?Pandas
import pandas as pd cols = pd.MultiIndex.from_tuples([('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two')]) data = [[1, 2, 3, 4], [5, 6, 7, 8]] df = pd.DataFrame(data, columns=cols) result = df.stack()
Attempts:
2 left
💡 Hint
stack() moves the innermost column level to become the innermost row index level.
✗ Incorrect
stack() converts the columns into a new inner row index level, preserving the outer row index. The result is a Series with a MultiIndex of the original row index and the stacked column levels.
❓ data_output
intermediate1:30remaining
What is the shape of the DataFrame after unstacking a Series with MultiIndex?
Consider a Series with MultiIndex of two levels and 6 elements. After applying
unstack() on the inner level, what will be the shape of the resulting DataFrame?Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]) data = pd.Series(range(6), index=index) result = data.unstack()
Attempts:
2 left
💡 Hint
unstack() pivots the inner index level to columns.
✗ Incorrect
The original Series has 3 outer index values ('a','b','c') and 2 inner index values (1,2). unstack() moves the inner level to columns, resulting in a DataFrame with 3 rows and 2 columns.
🔧 Debug
advanced1:30remaining
Why does this unstack() call raise a KeyError?
Given the DataFrame below, why does
df.unstack(level=2) raise a KeyError?Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')], names=['num', 'char']) df = pd.DataFrame({'value': [10, 20, 30, 40]}, index=index) result = df.unstack(level=2)
Attempts:
2 left
💡 Hint
Check the number of levels in the DataFrame's index.
✗ Incorrect
The MultiIndex has only two levels: 0 and 1. Asking to unstack level 2 causes a KeyError because that level does not exist.
🚀 Application
advanced2:00remaining
Transform a DataFrame with stacked sales data back to wide format
You have a DataFrame with sales data stacked by 'Store' and 'Product' as row indices and 'Month' as columns. Which code correctly unstacks the 'Product' level to columns?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('Store1', 'ProdA'), ('Store1', 'ProdB'), ('Store2', 'ProdA'), ('Store2', 'ProdB')], names=['Store', 'Product']) data = pd.DataFrame({'Jan': [100, 150, 200, 250], 'Feb': [110, 160, 210, 260]}, index=index) # Which code unstacks 'Product' to columns?
Attempts:
2 left
💡 Hint
unstack moves a row index level to columns.
✗ Incorrect
To move the 'Product' level from rows to columns, use unstack(level='Product'). stack() does the opposite.
🧠 Conceptual
expert2:30remaining
Understanding the effect of stack() and unstack() on DataFrame shape
If a DataFrame has shape (4, 3) with a simple index and columns, what will be the shape after applying
stack() and then unstack()?Attempts:
2 left
💡 Hint
stack() converts columns into a new inner row index level, unstack() reverses this.
✗ Incorrect
stack() converts each row's columns into a stacked Series with length rows * columns (4*3=12). unstack() reverses this back to the original shape (4,3).