📖 Scenario: You work in a small company that tracks monthly sales for different products. The sales data is stored in a table where each product has its own column for each month. This format is hard to analyze because the months are spread across columns.To make the data easier to analyze, you want to reshape it from wide format (many columns for months) to long format (one column for month and one for sales).
🎯 Goal: Learn how to use the pandas.melt() function to reshape a wide table into a long table. You will start with a dictionary of sales data, convert it to a DataFrame, then reshape it so each row shows a product, month, and sales value.
📋 What You'll Learn
Create a pandas DataFrame from a dictionary with product sales for three months
Define the columns to keep fixed during reshaping
Use pandas melt to reshape the DataFrame from wide to long format
Print the reshaped DataFrame
💡 Why This Matters
🌍 Real World
Data scientists often receive data in wide format, which is hard to analyze. Reshaping data to long format helps in plotting, grouping, and applying statistical methods.
💼 Career
Knowing how to reshape data with pandas melt is a key skill for data cleaning and preparation, essential for roles like data analyst, data scientist, and business intelligence specialist.
Progress0 / 4 steps
1
Create the sales data dictionary and DataFrame
Create a dictionary called sales_data with these exact entries: 'Product': ['A', 'B', 'C'], 'Jan': [100, 150, 200], 'Feb': [110, 160, 210], 'Mar': [120, 170, 220]. Then create a pandas DataFrame called df from sales_data.
Data Analysis Python
Hint
Use pd.DataFrame() to convert the dictionary to a DataFrame.
2
Define the id_vars for melting
Create a list called id_vars containing the column 'Product'. This tells pandas which column to keep fixed when reshaping.
Data Analysis Python
Hint
The id_vars list should include columns you want to keep as is.
3
Use pandas melt to reshape the DataFrame
Create a new DataFrame called df_long by using pd.melt() on df, with id_vars=id_vars, var_name='Month', and value_name='Sales'.
Data Analysis Python
Hint
Use pd.melt() with id_vars, var_name, and value_name parameters.
4
Print the reshaped DataFrame
Print the DataFrame df_long to see the data in long format.
Data Analysis Python
Hint
Use print(df_long) to display the reshaped DataFrame.
Practice
(1/5)
1. What does the melt function do in data analysis?
easy
A. It merges two dataframes horizontally.
B. It converts wide data format to long data format by stacking columns.
C. It filters rows based on a condition.
D. It sorts data rows based on column values.
Solution
Step 1: Understand data formats
Wide data has many columns representing variables; long data stacks these variables into fewer columns.
Step 2: Role of melt function
The melt function stacks columns into rows, converting wide data to long format.
Final Answer:
It converts wide data format to long data format by stacking columns. -> Option B
Quick Check:
melt = wide to long [OK]
Hint: Melt stacks columns into rows to reshape wide data [OK]
Common Mistakes:
Confusing melt with sorting or filtering
Thinking melt merges dataframes
Assuming melt changes row order only
2. Which of the following is the correct syntax to melt a dataframe df keeping the column 'ID' fixed?
easy
A. pd.melt(df, id_vars='ID')
B. pd.melt(df, fixed='ID')
C. df.melt(id='ID')
D. pd.melt(df, keep='ID')
Solution
Step 1: Identify correct parameter for fixed columns
The parameter to keep columns fixed during melt is id_vars.
Step 2: Check syntax correctness
Only pd.melt(df, id_vars='ID') uses the correct parameter and function call.
Final Answer:
pd.melt(df, id_vars='ID') -> Option A
Quick Check:
Use id_vars to keep columns fixed [OK]
Hint: Use id_vars to keep columns fixed in melt [OK]