Challenge - 5 Problems
Stack and Unstack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ data_output
intermediate2:00remaining
Output of stacking a DataFrame with multi-level columns
Given the following DataFrame with multi-level columns, what is the output after applying
df.stack(level=0).stack()?Data Analysis Python
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(level=0).stack()
Attempts:
2 left
💡 Hint
Stack moves the innermost column level to the row index, creating a Series with a MultiIndex.
✗ Incorrect
Stack converts the innermost column level into the innermost row index level. Here, the second level of columns ('one', 'two') becomes part of the row index, resulting in a Series indexed by the original row index and the first column level.
❓ data_output
intermediate2:00remaining
Unstacking a Series with MultiIndex
What is the output of unstacking the following Series
s.unstack(level=1)?Data Analysis Python
import pandas as pd index = pd.MultiIndex.from_tuples([(0, 'A'), (0, 'B'), (1, 'A'), (1, 'B')]) data = [10, 20, 30, 40] s = pd.Series(data, index=index) result = s.unstack(level=1)
Attempts:
2 left
💡 Hint
Unstack moves the specified level of the row index to columns.
✗ Incorrect
Unstacking level 1 moves the second level of the MultiIndex ('A', 'B') to columns, resulting in a DataFrame with index 0 and 1 and columns 'A' and 'B'.
🧠 Conceptual
advanced1:30remaining
Understanding the effect of stack on DataFrame shape
If a DataFrame has shape (4, 3) and you apply
df.stack(), what will be the shape of the resulting object?Attempts:
2 left
💡 Hint
Stack moves columns into the row index, flattening the DataFrame into a Series.
✗ Incorrect
Stack converts columns into a new inner row index level, so the total number of rows in the resulting Series is rows * columns = 4 * 3 = 12.
🔧 Debug
advanced1:30remaining
Identify the error in unstacking a Series
What error will this code raise and why?
import pandas as pd s = pd.Series([1, 2, 3], index=[0, 1, 2]) s.unstack()
Attempts:
2 left
💡 Hint
Unstack requires a MultiIndex to move one level to columns.
✗ Incorrect
Unstack tries to pivot one level of a MultiIndex to columns. Here, the Series has a simple Index with only one level, so unstacking fails with IndexError.
🚀 Application
expert2:30remaining
Transforming a DataFrame with stack and unstack to swap index and columns
You have a DataFrame
df with shape (3, 4). You want to swap its rows and columns using stack and unstack only (no transpose). Which sequence of operations will achieve this?Attempts:
2 left
💡 Hint
Stack moves columns to rows, unstack moves rows to columns. Use level parameters carefully.
✗ Incorrect
Stack moves the innermost column level to the row index, then unstack with level=0 moves the original row index to columns, effectively swapping rows and columns.