9️⃣ 🔄 Pandas Reshaping & Pivoting Data
Estimated reading: 3 minutes 72 views

🧱 Pandas Stacking & Unstacking – Reshape MultiIndex DataFrames Like a Pro


🧲 Introduction – Why Use Stacking and Unstacking?

Stacking and unstacking are powerful reshaping tools in Pandas that convert columns into rows and rows into columns, especially useful when working with MultiIndex DataFrames. These operations are foundational for pivoting, flattening, and tidying data for analysis or visualization.

🎯 In this guide, you’ll learn:

  • Use .stack() to pivot columns into a deeper index
  • Use .unstack() to pivot index levels into columns
  • Reshape and tidy MultiIndex DataFrames
  • Control which level to stack/unstack

📥 1. Create a MultiIndex DataFrame

import pandas as pd

index = pd.MultiIndex.from_tuples([
    ('HR', 2023),
    ('HR', 2024),
    ('IT', 2023),
    ('IT', 2024)
], names=['Department', 'Year'])

df = pd.DataFrame({
    'Budget': [50000, 52000, 60000, 63000],
    'Headcount': [10, 12, 15, 17]
}, index=index)

👉 Output:

                  Budget  Headcount
Department Year                    
HR         2023    50000         10
           2024    52000         12
IT         2023    60000         15
           2024    63000         17

🔄 2. Stack – Convert Columns into Index Level

stacked = df.stack()

👉 Output:

Department  Year           
HR          2023    Budget       50000
                    Headcount      10
            2024    Budget       52000
                    Headcount      12
IT          2023    Budget       60000
                    Headcount      15
            2024    Budget       63000
                    Headcount      17
dtype: int64

✔️ Column labels become an inner index level (creating a Series with 3-level MultiIndex).


📤 3. Unstack – Convert Index Level into Columns

df_unstacked = stacked.unstack()

✔️ Reverts .stack() operation, restoring original DataFrame.


🧱 4. Unstack Specific Level

df.unstack(level='Year')

👉 Output:

           Budget           Headcount        
Year        2023  2024      2023  2024
Department                            
HR         50000  52000        10    12
IT         60000  63000        15    17

✔️ Moves 'Year' from index to columns.


🏗️ 5. Stack Specific Level (e.g., Column MultiIndex)

df_unstacked.stack(level='Year')

✔️ Restacks the specific level back into row index.


🧽 6. Reset Index After Stack

df_reset = df.stack().reset_index(name='Value')

✔️ Converts stacked Series into a flat DataFrame.


🚫 7. Handle Missing Values When Unstacking

If unstacking results in missing combinations, Pandas fills them with NaN.

df_miss = df.drop(('IT', 2024))
df_miss.unstack()

👉 Use .fillna() after unstacking to handle gaps.


📌 Summary – Key Takeaways

Stacking and unstacking are essential for reshaping MultiIndex DataFrames between wide and long formats. These tools are flexible and allow level-specific transformations for advanced data wrangling.

🔍 Key Takeaways:

  • .stack() moves columns into inner index levels
  • .unstack() moves index levels into columns
  • Specify level to control which level to transform
  • Combine with .reset_index() or .melt() for flattening
  • Missing entries after unstack result in NaN

⚙️ Real-world relevance: Used in pivot table transformations, hierarchical reports, tidy data modeling, and cross-tab reshaping.


❓ FAQs – Pandas Stacking and Unstacking

❓ What’s the difference between stack and melt?

  • .stack() is for MultiIndex reshaping
  • .melt() is for flattening wide to long format from standard DataFrames

❓ Can I stack a regular DataFrame?
Yes, stacking a flat DataFrame converts columns into a row index level.


❓ What if I have missing data after unstack?
You’ll get NaNs. Fill them using .fillna():

df.unstack().fillna(0)

❓ Can I stack or unstack columns with MultiIndex?
✅ Yes, use axis=1 in .stack() or .unstack() to reshape column levels.


❓ Is stacking reversible?
Yes. .stack().unstack() or .unstack().stack() will restore the original if no data is lost.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Pandas Stacking & Unstacking

Or Copy Link

CONTENTS
Scroll to Top