Recall & Review
beginner
What does the
stack() method do in a DataFrame?It pivots the columns of a DataFrame into the index, turning columns into rows and creating a Series with a MultiIndex.
Click to reveal answer
beginner
What is the purpose of the
unstack() method in pandas?It pivots the innermost level of the index back into columns, turning a Series or DataFrame from a stacked format to a wider format.
Click to reveal answer
intermediate
How does
stack() affect the shape of a DataFrame?It reduces the number of columns by moving them into the row index, increasing the number of rows accordingly.
Click to reveal answer
intermediate
What happens if you use
unstack(level=1) on a MultiIndex Series?It moves the second level of the index (level 1) into columns, reshaping the data to a wider format based on that level.
Click to reveal answer
beginner
Why are
stack() and unstack() useful in data analysis?They help reshape data for easier analysis, visualization, or aggregation by switching between wide and long formats.
Click to reveal answer
What does the
stack() method do to a DataFrame?✗ Incorrect
stack() moves columns into the row index, turning columns into rows.
Which method reverses the effect of
stack()?✗ Incorrect
unstack() pivots the index back into columns, reversing stack().
If a DataFrame has shape (3, 4), what will be the shape after
stack()?✗ Incorrect
stack() moves 4 columns into rows, so rows become 3*4=12 and columns become 1.
What argument can you pass to
unstack() to specify which index level to pivot?✗ Incorrect
The level argument specifies which index level to unstack.
Which of these is a common use case for
stack() and unstack()?✗ Incorrect
They are used to reshape data, switching between wide and long formats.
Explain how
stack() and unstack() work in pandas and why they are useful.Think about how data layout changes between rows and columns.
You got /4 concepts.
Describe a real-life example where you might use
stack() and unstack() to prepare data for analysis.Imagine you have survey data with multiple answers per person.
You got /4 concepts.