0
0
Data-analysis-pythonHow-ToBeginner ยท 3 min read

How to Melt Data in Python Using pandas melt Function

To melt data in Python, use the pandas.melt() function which transforms wide-format data into long-format by unpivoting selected columns. You specify the identifier columns with id_vars and the columns to unpivot with value_vars.
๐Ÿ“

Syntax

The pandas.melt() function syntax is:

  • frame: The DataFrame to melt.
  • id_vars: Columns to keep as identifiers (not unpivoted).
  • value_vars: Columns to unpivot into rows.
  • var_name: Name for the new variable column (optional).
  • value_name: Name for the new value column (optional).
python
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value')
๐Ÿ’ป

Example

This example shows how to convert a wide DataFrame with sales data for different years into a long format using pandas.melt().

python
import pandas as pd

data = {
    'Store': ['A', 'B', 'C'],
    '2019': [100, 150, 200],
    '2020': [110, 160, 210],
    '2021': [120, 170, 220]
}
df = pd.DataFrame(data)

melted = pd.melt(df, id_vars=['Store'], value_vars=['2019', '2020', '2021'],
                 var_name='Year', value_name='Sales')
print(melted)
Output
Store Year Sales 0 A 2019 100 1 B 2019 150 2 C 2019 200 3 A 2020 110 4 B 2020 160 5 C 2020 210 6 A 2021 120 7 B 2021 170 8 C 2021 220
โš ๏ธ

Common Pitfalls

Common mistakes when melting data include:

  • Not specifying id_vars, which causes all columns to be unpivoted.
  • Forgetting to set value_vars when you want to melt only specific columns.
  • Not renaming var_name and value_name, which can make the output confusing.

Here is an example of a common mistake and the correct way:

python
import pandas as pd

data = {'Name': ['John', 'Anna'], 'Math': [90, 95], 'Science': [85, 88]}
df = pd.DataFrame(data)

# Wrong: melts all columns including 'Name'
wrong = pd.melt(df)
print('Wrong melt output:\n', wrong)

# Right: keep 'Name' as id_vars
right = pd.melt(df, id_vars=['Name'], value_vars=['Math', 'Science'],
                var_name='Subject', value_name='Score')
print('\nRight melt output:\n', right)
Output
Wrong melt output: variable value 0 Name John 1 Name Anna 2 Math 90 3 Math 95 4 Science 85 5 Science 88 Right melt output: Name Subject Score 0 John Math 90 1 Anna Math 95 2 John Science 85 3 Anna Science 88
๐Ÿ“Š

Quick Reference

Remember these tips when using pandas.melt():

  • Use id_vars to keep columns fixed.
  • Use value_vars to select columns to unpivot.
  • Rename columns with var_name and value_name for clarity.
  • Melting helps reshape data for easier analysis and plotting.
โœ…

Key Takeaways

Use pandas.melt() to convert wide data into long format by unpivoting columns.
Always specify id_vars to keep identifier columns unchanged.
Select only the columns to melt with value_vars to avoid unwanted data changes.
Rename the output columns with var_name and value_name for better readability.
Melting data is useful for preparing data for analysis and visualization.