Bird
Raised Fist0
Data Analysis Pythondata~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does the stack() method do to a DataFrame in pandas?
      easy
      A. It moves columns into rows, making the data longer.
      B. It moves rows into columns, making the data wider.
      C. It sorts the DataFrame by index.
      D. It removes duplicate rows from the DataFrame.

      Solution

      1. Step 1: Understand the stack operation

        The stack() method compresses columns into a single column, turning columns into rows.
      2. Step 2: Compare with unstack

        Unlike unstack(), which moves rows to columns, stack() makes data longer by stacking columns.
      3. Final Answer:

        It moves columns into rows, making the data longer. -> Option A
      4. Quick Check:

        Stack = columns to rows [OK]
      Hint: Stack compresses columns into rows, making data longer [OK]
      Common Mistakes:
      • Confusing stack with unstack direction
      • Thinking stack sorts data
      • Assuming stack removes duplicates
      2. Which of the following is the correct syntax to unstack the last level of a MultiIndex DataFrame df?
      easy
      A. df.stack(axis=0)
      B. df.stack(level=-1)
      C. df.unstack(level=-1)
      D. df.unstack(axis=1)

      Solution

      1. Step 1: Recall unstack syntax

        The unstack() method by default unstacks the last level of the index, and level=-1 explicitly specifies this.
      2. Step 2: Differentiate from stack

        stack() is the opposite operation, so options A and B are incorrect. unstack() does not take an axis parameter, so df.unstack(axis=1) is invalid.
      3. Final Answer:

        df.unstack(level=-1) -> Option C
      4. Quick Check:

        Unstack last level = df.unstack(level=-1) [OK]
      Hint: Use unstack(level=-1) to unstack last index level [OK]
      Common Mistakes:
      • Using stack instead of unstack
      • Passing axis parameter to unstack
      • Not specifying level when needed
      3. Given the DataFrame df below:
      import pandas as pd
      d = {('A', 'x'): 1, ('A', 'y'): 2, ('B', 'x'): 3, ('B', 'y'): 4}
      df = pd.Series(d).unstack()

      What is the output of df.stack()?
      medium
      A. A DataFrame with columns 'A' and 'B' stacked
      B. A Series with MultiIndex where second level is stacked into rows
      C. A DataFrame with columns 'x' and 'y' unchanged
      D. An error because stack cannot be applied here

      Solution

      1. Step 1: Understand the initial DataFrame

        The unstack() converts the Series with MultiIndex into a DataFrame with columns 'x' and 'y' and index 'A' and 'B'.
      2. Step 2: Apply stack() on the DataFrame

        Calling stack() moves the columns 'x' and 'y' back into the inner level of the row index, producing a Series with MultiIndex.
      3. Final Answer:

        A Series with MultiIndex where second level is stacked into rows -> Option B
      4. Quick Check:

        Stack reverses unstack, making columns into row index [OK]
      Hint: Stack reverses unstack, turning columns back into row index [OK]
      Common Mistakes:
      • Expecting a DataFrame output instead of Series
      • Confusing which index level is stacked
      • Thinking stack causes error here
      4. You run the code below and get an error:
      df = pd.DataFrame({
        'A': [1, 2],
        'B': [3, 4]
      })
      stacked = df.stack(level=1)

      What is the cause of the error?
      medium
      A. The DataFrame index is missing.
      B. Stack requires numeric data only.
      C. Stack cannot be used on DataFrames, only Series.
      D. DataFrame columns have no MultiIndex, so level=1 is invalid.

      Solution

      1. Step 1: Check DataFrame columns

        The DataFrame has simple columns 'A' and 'B', which are not a MultiIndex, so specifying level=1 is invalid.
      2. Step 2: Understand stack level parameter

        The level parameter in stack() refers to the level of the columns MultiIndex to stack. Without MultiIndex columns, level=1 causes an error.
      3. Final Answer:

        DataFrame columns have no MultiIndex, so level=1 is invalid. -> Option D
      4. Quick Check:

        Stack level requires MultiIndex columns [OK]
      Hint: Stack level needs MultiIndex columns; simple columns cause error [OK]
      Common Mistakes:
      • Assuming stack only works on Series
      • Thinking stack requires numeric data
      • Ignoring the column index type
      5. You have a DataFrame df with a MultiIndex on rows (levels: 'City', 'Year') and columns as years (e.g., 2020, 2021). You want to reshape it so that the 'Year' level from rows becomes columns, and the columns years become a new row level. Which sequence of operations correctly achieves this?
      hard
      A. First unstack the 'Year' level from rows, then stack the columns.
      B. First stack the columns, then unstack the 'City' level from rows.
      C. Use only stack on the columns without unstacking rows.
      D. Use only unstack on the 'City' level.

      Solution

      1. Step 1: Unstack the 'Year' level from rows

        Unstacking the 'Year' level moves it from the row index to columns, widening the DataFrame.
      2. Step 2: Stack the columns

        Stacking the columns moves the columns years into a new row index level, making the data longer again but reshaped as desired.
      3. Final Answer:

        First unstack the 'Year' level from rows, then stack the columns. -> Option A
      4. Quick Check:

        Unstack rows then stack columns reshapes MultiIndex correctly [OK]
      Hint: Unstack row level first, then stack columns to reshape MultiIndex [OK]
      Common Mistakes:
      • Stacking before unstacking causes wrong shape
      • Unstacking wrong index level
      • Ignoring the need to reshape both rows and columns
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard