
Member-only story
Data Science, Editorial, Programming
Understanding Pandas Melt — pd.melt()
Understanding the most efficient and flexible function to reshape Pandas data frames
Author(s): Pratik Shukla, Roberto Iriondo
This tutorial will dive deeper into Pandas’ pd.melt()
function to understand its core functionalities with graphics and its implementation in Python. We will first see the syntax and parameters for this method. Then we will take a few examples to understand all the pd.melt()
function parameters. The companion resources to this tutorial can be found either on Google Colab or Github.
What is PD Melt?
The Pandas melt() function is within many other methods used to reshape the pandas DataFrames from wide to a long format which is particularly useful in data science. However, the pd.melt()
function is the most efficient and flexible among them. The pd.melt()
function unpivots/melts the pandas DataFrame from a wide to a long format.

Wide versus Long DataFrame:
Before converting our wide DataFrame to a long DataFrame, let us first visually see the difference between them. The following is an example of a DataFrame in wide form.

Now, the following image represents a DataFrame that contains the same data but in a long-form.

Before diving deeper into it, let us first create a wide DataFrame using pd.DataFrame()
.