In data analysis, reshaping refers to transforming a dataset between:
Long ↔︎ Wide layouts, where repeated measures can be stacked as rows or spread across columns
Managing composite variable names by splitting them into multiple fields or combining fields into a single label
In Python (pandas), these tasks use well-known tools.
Operation
pandas tool
Wide → Long
melt()
Long → Wide
pivot() or pivot_table()
Split a column
str.split(..., expand=True)
Combine multiple columns
string concatenation (+)
Example Dataset
import pandas as pd# A simple dataset with two subject-wise columnsdf = pd.DataFrame({"id": [1, 2, 3],"math": [80, 85, 90],"english": [70, 78, 88]})df
id
math
english
0
1
80
70
1
2
85
78
2
3
90
88
2 Long Format (Wide → Long)
Transform the subject-wise columns into key–value rows.
df_long = df.melt( id_vars="id", # keep 'id' as identifier column value_vars=["math", "english"], # columns to unpivot var_name="subject", # new column storing original column names value_name="score"# new column storing values)df_long
id
subject
score
0
1
math
80
1
2
math
85
2
3
math
90
3
1
english
70
4
2
english
78
5
3
english
88
3 Wide Format (Long → Wide)
Starting from the long dataset:
df_long
id
subject
score
0
1
math
80
1
2
math
85
2
3
math
90
3
1
english
70
4
2
english
78
5
3
english
88
df_wide = df_long.pivot( index="id", # one row per id columns="subject", # create new columns from subject names values="score"# fill cells with score values).reset_index() # bring index back as a regular columndf_wide
subject
id
english
math
0
1
70
80
1
2
78
85
2
3
88
90
4 Splitting a Column into Multiple Fields
Suppose you have a dataset where people’s full names, separated by commas, are stored in a single column: