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.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 15 views 0 copies

Requires third-party packages — install first
pip install pandas

Python code

24 lines
Python 3.9+
import 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

stdout
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

  1. Use `df_wide.melt(id_vars=['id', 'name'], var_name='subject', value_name='score')` as a method on the DataFrame.
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Data pipelines & processing

Related tutorials and quizzes for this topic.