0
0
Pandasdata~5 mins

Wide to long format conversion in Pandas

Choose your learning style9 modes available
Introduction

We change data from wide to long format to make it easier to analyze and visualize. Long format stacks related data into fewer columns, which many tools prefer.

You have survey data with one column per question and want one row per answer.
You want to plot time series data where each time point is a separate column.
You need to prepare data for statistical tests that require long format.
You want to group and summarize data by categories stored in column names.
Syntax
Pandas
pd.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)

frame is your original DataFrame.

id_vars are columns to keep as identifiers (not unpivoted).

Examples
This keeps the 'Name' column and converts 'Math' and 'Science' columns into two rows per name.
Pandas
pd.melt(df, id_vars=['Name'], value_vars=['Math', 'Science'])
Renames the new variable column to 'Subject' and the values column to 'Score'.
Pandas
pd.melt(df, id_vars=['ID'], var_name='Subject', value_name='Score')
Sample Program

This example converts a wide DataFrame with scores in separate columns into a long format with one score per row.

Pandas
import pandas as pd

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

long_df = pd.melt(df, id_vars=['Name'], var_name='Subject', value_name='Score')
print(long_df)
OutputSuccess
Important Notes

Wide format has many columns for related data; long format stacks them into rows.

Use id_vars to keep columns that identify each row.

Renaming var_name and value_name makes the output clearer.

Summary

Wide to long conversion stacks columns into rows for easier analysis.

Use pd.melt() with id_vars to keep identifier columns.

Renaming columns in the output helps understand the data better.