0
0
Data Analysis Pythondata~20 mins

Stack and unstack in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack and Unstack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
data_output
intermediate
2: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()
A
0  A  one    1
         two    2
   B  one    3
         two    4
1  A  one    5
         two    6
   B  one    7
         two    8
dtype: int64
B{0: {('A', 'one'): 1, ('A', 'two'): 2, ('B', 'one'): 3, ('B', 'two'): 4}, 1: {('A', 'one'): 5, ('A', 'two'): 6, ('B', 'one'): 7, ('B', 'two'): 8}}
C
A  one  0    1
          1    5
   two  0    2
          1    6
B  one  0    3
          1    7
   two  0    4
          1    8
dtype: int64
DMultiIndex([(0, 'A', 'one'), (0, 'A', 'two'), (0, 'B', 'one'), (0, 'B', 'two'), (1, 'A', 'one'), (1, 'A', 'two'), (1, 'B', 'one'), (1, 'B', 'two')], names=[None, None, None]) with values [1, 2, 3, 4, 5, 6, 7, 8]
Attempts:
2 left
💡 Hint
Stack moves the innermost column level to the row index, creating a Series with a MultiIndex.
data_output
intermediate
2: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)
A
   0   1
A  10  30
B  20  40
B
0  A    10
   B    20
1  A    30
   B    40
dtype: int64
C
A  0    10
   1    30
B  0    20
   1    40
dtype: int64
D
   A   B
0  10  20
1  30  40
Attempts:
2 left
💡 Hint
Unstack moves the specified level of the row index to columns.
🧠 Conceptual
advanced
1: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?
AA Series with length 12
BA DataFrame with shape (3, 4)
CA DataFrame with shape (4, 3)
DA Series with length 7
Attempts:
2 left
💡 Hint
Stack moves columns into the row index, flattening the DataFrame into a Series.
🔧 Debug
advanced
1: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()
AValueError: Index contains duplicate entries, cannot unstack
BIndexError: Too many levels: Index has only 1 level, not 2
CAttributeError: 'Series' object has no attribute 'unstack'
DTypeError: unstack() missing required argument 'level'
Attempts:
2 left
💡 Hint
Unstack requires a MultiIndex to move one level to columns.
🚀 Application
expert
2: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?
A<code>df.stack(level=1).unstack(level=0)</code>
B<code>df.unstack().stack(level=1)</code>
C<code>df.stack().unstack(level=0)</code>
D<code>df.unstack(level=0).stack(level=1)</code>
Attempts:
2 left
💡 Hint
Stack moves columns to rows, unstack moves rows to columns. Use level parameters carefully.