0
0
Data Analysis Pythondata~10 mins

Melt for wide-to-long reshaping in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Melt for wide-to-long reshaping
Start with wide DataFrame
Select id_vars and value_vars
Apply pd.melt()
Create long DataFrame with columns: id_vars + variable + value
Use long DataFrame for analysis or visualization
This flow shows how we start with a wide table, choose which columns to keep fixed (id_vars) and which to reshape (value_vars), then apply melt to get a long table.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Name': ['Anna', 'Bob'],
    'Math': [90, 80],
    'Science': [85, 88]
})

long_df = pd.melt(df, id_vars=['Name'], var_name='Subject', value_name='Score')
This code reshapes a wide DataFrame of student scores into a long format with one score per row.
Execution Table
StepActionDataFrame ShapeDataFrame Preview
1Create wide DataFrame(2, 3)[{'Name': 'Anna', 'Math': 90, 'Science': 85}, {'Name': 'Bob', 'Math': 80, 'Science': 88}]
2Call pd.melt with id_vars=['Name'](4, 3)[{'Name': 'Anna', 'Subject': 'Math', 'Score': 90}, {'Name': 'Bob', 'Subject': 'Math', 'Score': 80}, {'Name': 'Anna', 'Subject': 'Science', 'Score': 85}, {'Name': 'Bob', 'Subject': 'Science', 'Score': 88}]
3Result is long DataFrame(4, 3)[{'Name': 'Anna', 'Subject': 'Math', 'Score': 90}, {'Name': 'Bob', 'Subject': 'Math', 'Score': 80}, {'Name': 'Anna', 'Subject': 'Science', 'Score': 85}, {'Name': 'Bob', 'Subject': 'Science', 'Score': 88}]
💡 All wide columns except 'Name' melted into 'Subject' and 'Score' columns, reshaping complete.
Variable Tracker
VariableStartAfter pd.meltFinal
df[{'Name': 'Anna', 'Math': 90, 'Science': 85}, {'Name': 'Bob', 'Math': 80, 'Science': 88}]N/ASame as start
long_dfN/A[{'Name': 'Anna', 'Subject': 'Math', 'Score': 90}, {'Name': 'Bob', 'Subject': 'Math', 'Score': 80}, {'Name': 'Anna', 'Subject': 'Science', 'Score': 85}, {'Name': 'Bob', 'Subject': 'Science', 'Score': 88}]Same as after pd.melt
Key Moments - 3 Insights
Why do we specify 'id_vars' in pd.melt?
The 'id_vars' columns stay fixed and identify each row in the long format. Without them, we lose the link to original rows. See execution_table step 2 where 'Name' stays as is.
What happens to columns not in 'id_vars' during melt?
They become part of two new columns: one for the original column names ('variable') and one for their values ('value'). In our example, 'Math' and 'Science' become 'Subject' and 'Score' columns (execution_table step 2).
Why does the number of rows increase after melting?
Because each original row with multiple value columns turns into multiple rows, one per value column. Here, 2 rows with 2 score columns become 4 rows (execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the value of 'Subject' for the third row in long_df?
AMath
BScience
CName
DScore
💡 Hint
Check the 'DataFrame Preview' column in execution_table step 2; the third row has 'Subject' as 'Science'.
At which step does the DataFrame shape change from (2, 3) to (4, 3)?
AStep 2
BStep 3
CStep 1
DNever
💡 Hint
Look at the 'DataFrame Shape' column in execution_table; shape changes at step 2.
If we did not specify 'id_vars' in pd.melt, what would happen to the 'Name' column?
AIt would stay as a fixed column
BIt would be dropped
CIt would be melted into variable and value columns
DIt would cause an error
💡 Hint
Recall key_moments about 'id_vars' role; without it, all columns except index are melted.
Concept Snapshot
pd.melt reshapes wide DataFrames to long format.
Use id_vars to keep columns fixed.
value_vars are melted into variable and value columns.
Result has more rows, fewer columns.
Useful for tidy data and plotting.
Full Transcript
We start with a wide DataFrame containing student names and their scores in Math and Science. Using pd.melt, we specify 'Name' as the id_vars to keep it fixed. The other columns, 'Math' and 'Science', are melted into two new columns: 'Subject' for the original column names and 'Score' for their values. This reshapes the data from wide to long format, increasing the number of rows. The execution table shows the DataFrame shape and preview at each step. Key moments clarify why id_vars are needed and how columns transform. The visual quiz tests understanding of the reshaping process and the role of parameters.