Course contentsShow
← Python
Lesson 951 of 1,06015. Data Science FundamentalsFree lesson

Restructuring DataFrames with Melt

Convert wide-format DataFrames into a tidy long-format by unpivoting columns into key-value pairs for streamlined data processing.

In this lesson, you will learn how to transform "wide-format" datasets into a "tidy" long-format structure using the melt() function to make your data ready for analysis.

Understanding the Wide vs. Long Format

Often, datasets come in a "wide" format, where variables are spread across multiple columns. For example, consider a DataFrame recording temperature across different months:

CityJanFebMar
London568
Paris7810

While easy for humans to read, this format is difficult for many analytical tools and visualization libraries. A "tidy" (long) format, by contrast, ensures each variable is a column and each observation is a row. To achieve this, we "unpivot" the month columns into two new columns: one representing the variable (Month) and one representing the value (Temperature).

Implementing pd.melt()

The melt() function is the primary tool for this restructuring. You specify the id_vars (the column(s) you want to keep as identifiers) and the value_vars (the columns you want to collapse).

import pandas as pd

df = pd.DataFrame({
    'City': ['London', 'Paris'],
    'Jan': [5, 7],
    'Feb': [6, 8],
    'Mar': [8, 10]
})

# Melt the DataFrame
tidy_df = df.melt(
    id_vars=['City'], 
    value_vars=['Jan', 'Feb', 'Mar'], 
    var_name='Month', 
    value_name='Temperature'
)

In this output, London and Paris will repeat for each month, effectively creating a vertical list of observations. The var_name parameter assigns a name to the column containing the old column headers, while value_name labels the column containing the cell values.

This transformation is essential for grouping, aggregating, or plotting data, as it allows your code to treat the month as a distinct feature rather than a collection of separate columns. By shifting from columns to rows, you normalize your dataset, making it compatible with high-performance operations like those we've explored in earlier lessons.

Key Takeaway: The melt() function reshapes wide-format data into a tidy, long format by converting specific columns into key-value pairs, which is the prerequisite for robust statistical analysis and visualization.