0PricingLogin
Pandas & NumPy Academy · Lesson

melt: Wide to Long Format

Convert a wide DataFrame to long format with pd.melt, specifying id_vars and value_vars.

Wide vs Long Data Formats

Data can be structured in two fundamental shapes. Wide format has one row per subject and spreads measurements across multiple columns — for example, separate columns for January, February, and March sales. Long format has one row per observation, with a column for the variable name and another for the value. Most Pandas analysis functions, machine learning libraries, and visualisation tools prefer long format, making melt() an essential transformation tool.

The pd.melt() Function

pd.melt(df) (also available as df.melt()) converts a wide DataFrame to long format. The key parameters are id_vars (columns to keep as identifiers, unchanged), value_vars (columns to unpivot into rows), var_name (name for the new variable column), and value_name (name for the new value column).

import pandas as pd

# Wide format: separate columns per month
df_wide = pd.DataFrame({
    'product': ['A', 'B'],
    'Jan':     [100, 150],
    'Feb':     [120, 130],
    'Mar':     [140, 160]
})

print(df_wide)
#   product  Jan  Feb  Mar
# 0       A  100  120  140
# 1       B  150  130  160

All lessons in this course

  1. pivot_table: Cross-Tabulation
  2. melt: Wide to Long Format
  3. stack and unstack with MultiIndex
  4. crosstab for Frequency Tables
← Back to Pandas & NumPy Academy