MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Reshaping Data: Melt, Stack & Unstack0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
7.1Concatenating DataFrames (pd.concat)7.2Relational Joins with pd.merge (Inner, Left, Right, Outer)7.3Reshaping Data: Melt, Stack & Unstack
HomeTutorialsPandasReshaping Data: Melt, Stack & Unstack
DateTime Parsing, Indexing & .dt Accessor

Reshaping Data: Melt, Stack & Unstack

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Wide Format vs Long (Tidy) Format

In data analytics, data typically arrives in one of two formats:

  • Wide Format: Human-friendly tables where dates or categories are spread horizontally across multiple column headers (common in Excel financial reports).
  • Long / Tidy Format: Machine-friendly format where each variable is a column and each observation is a single row (essential for databases, Tableau, Power BI, and Seaborn).

2. Converting Wide to Long with pd.melt()

pd.melt() unpivots a DataFrame from wide format to long format:

1import pandas as pd
2 
3# Wide Excel-style report
4df_wide = pd.DataFrame({
5 'City': ['Delhi', 'Mumbai'],
6 'Q1_Sales': [45000, 72000],
7 'Q2_Sales': [52000, 68000],
8 'Q3_Sales': [61000, 81000]
9})
10print("Wide Table:\n", df_wide)
11 
12# Unpivot into Tidy Long Format
13df_long = pd.melt(
14 df_wide,
15 id_vars=['City'], # Identifier columns to keep fixed
16 value_vars=['Q1_Sales', 'Q2_Sales', 'Q3_Sales'], # Columns to unpivot into rows
17 var_name='Quarter', # New column name for former column headers
18 value_name='Revenue_INR' # New column name for the values
19)
20print("\nLong Tidy Table:\n", df_long)

Output:

1Long Tidy Table:
2 City Quarter Revenue_INR
30 Delhi Q1_Sales 45000
41 Mumbai Q1_Sales 72000
52 Delhi Q2_Sales 52000
63 Mumbai Q2_Sales 68000
74 Delhi Q3_Sales 61000
85 Mumbai Q3_Sales 81000

3. Reshaping with stack() & unstack()

  • stack(): Pivots the innermost column level to the innermost row index level (makes DataFrame taller and narrower).
  • unstack(): Pivots the innermost row index level to the innermost column level (makes DataFrame shorter and wider).
1# Stacking moves columns into a MultiIndex Series
2stacked = df_wide.set_index('City').stack()
3 
4# Unstacking restores it back to columns
5restored = stacked.unstack()

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which function converts a wide DataFrame into a long (tidy) DataFrame by unpivoting columns into rows?
Question 2
In pd.melt(df, id_vars=['City']), what do the columns in id_vars represent?
Question 3
What does df.unstack() do?
Question 4
Why is 'tidy' (long) data preferred when exporting data to Tableau or Power BI?
Question 5
In pd.melt(), which parameters customize the names of the resulting melted columns?

Next Lesson

DateTime Parsing, Indexing & .dt Accessor

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Relational Joins with pd.merge (Inner, Left, Right, Outer)DateTime Parsing, Indexing & .dt Accessor

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext