How to Unpivot Wide to Long with pandas melt in Python
This code demonstrates how to use pandas.melt to unpivot a wide DataFrame into a tidy long format, converting subject columns into rows.
pip install pandas
Python code
24 linesimport pandas as pd
# Sample wide-format data
df_wide = pd.DataFrame({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'math': [90, 85, 95],
'science': [80, 92, 88]
})
print("Original wide DataFrame:")
print(df_wide)
# Melt: unpivot subject columns into rows
df_long = pd.melt(
df_wide,
id_vars=['id', 'name'],
value_vars=['math', 'science'],
var_name='subject',
value_name='score'
)
print("\nLong (melted) DataFrame:")
print(df_long)
Output
Original wide DataFrame:
id name math science
0 1 Alice 90 80
1 2 Bob 85 92
2 3 Charlie 95 88
Long (melted) DataFrame:
id name subject score
0 1 Alice math 90
1 2 Bob math 85
2 3 Charlie math 95
3 1 Alice science 80
4 2 Bob science 92
5 3 Charlie science 88
How it works
The pd.melt function reshapes the DataFrame from wide to long format by unpivoting specified columns into two new columns: var_name for the original column names and value_name for the corresponding values. The id_vars parameter keeps the identifying columns ('id', 'name') fixed, while value_vars selects which columns to unpivot. This operation is fundamental for data cleaning and preparation, especially when transitioning to a tidy data structure where each row represents a single observation. The resulting long format is easier to analyze with group-by operations, plotting libraries, and statistical models.
Common mistakes
- Forgetting to include all identifier columns in `id_vars`, causing loss of context.
- Not specifying `value_vars`, which melts all remaining columns and may produce unexpected results.
- Assuming `pd.melt` modifies the original DataFrame; it returns a new DataFrame instead.
- Using `melt` with duplicate index values without resetting the index first.
Variations
- Use `df_wide.melt(id_vars=['id', 'name'], var_name='subject', value_name='score')` as a method on the DataFrame.
- Set `col_level` and `col_level` parameters when melting MultiIndex columns to control which column levels become the variable.
Real-world use cases
- Preparing survey responses where each question column needs to become a row for analysis.
- Transforming multi-year sales data from wide columns into a long format for time-series plotting.
- Reshaping experimental results with multiple metrics into a tidy data structure for statistical testing.
Sponsored
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.