Using melt() for unpivoting data in pandas
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. The sales data is recorded in a table where each column is a bread type and each row is a day. You want to reshape this data to a long format to analyze sales more easily.
🎯 Goal: Learn how to use the pandas melt() function to unpivot a wide table into a long table with one column for bread type and one for sales.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data for three bread types.
Define the columns to keep fixed during unpivoting.
Use
melt() to transform the DataFrame from wide to long format.Print the final melted DataFrame.
💡 Why This Matters
🌍 Real World
Unpivoting data is common when you want to convert wide tables into long tables for easier analysis and visualization.
💼 Career
Data analysts and scientists often reshape data using melt() to prepare it for charts, reports, or machine learning.
Progress0 / 4 steps