0
0
Data Analysis Pythondata~5 mins

Stack and unstack in Data Analysis Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ADeletes missing values
BTurns rows into columns
CTurns columns into rows by moving them into the index
DSorts the DataFrame by index
Which method reverses the effect of stack()?
Aunstack()
Bpivot()
Cmelt()
Dgroupby()
If a DataFrame has shape (3, 4), what will be the shape after stack()?
A(1, 12)
B(3, 4)
C(4, 3)
D(12, 1)
What argument can you pass to unstack() to specify which index level to pivot?
Alevel
Baxis
Ccolumns
Dindex
Which of these is a common use case for stack() and unstack()?
AChanging data types
BReshaping data between wide and long formats
CFiltering rows
DSorting columns
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.