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 160All lessons in this course
- pivot_table: Cross-Tabulation
- melt: Wide to Long Format
- stack and unstack with MultiIndex
- crosstab for Frequency Tables