Melt function python – Melt and Unmelt Data using Pandas melt() and pivot() Function

Melt function python: This tutorial will go through how to use the melt() and pivot() functions to melt and unmelt data values in a Pandas data frame.

Pandas Melting and Unmelting data

Python pandas melt: Before delving into the topic of melting and unmelting data, I’d like to draw your attention to this enticing word: Ice-Cream.
Yes! Ice-cream… When you’ve got it on your plate and you get a call just as you’re ready to take the first bite. Obviously, the ice cream will melt and turn into a milkshake.

Consider the concept of Melting data values in a data frame, which is comparable. Data value melting is used to customize and change data values from a wider format to a more restricted and long format. The primary goal of Melting is to generate a specific format of Dataframe in which one or more data columns serve as identifiers for the data attributes.

In this case, the remaining data variables are treated as data values, and there are only two columns: variable and value.

Unmelting, on the other hand, is performed on the data variables to return the values to their original format.

After we’ve covered Melting and Unmelting data, let’s look at the Pandas functions that allow us to do the same.

Pandas Module

Python pandas melt: Pandas is a Python-based open-source library with a BSD license. Pandas offer high-performance, quick, and simple data structures and data analysis tools for manipulating numeric data and time series. Pandas is based on the Numpy library and is developed in Python, Cython, and C. Wes McKinney created the Pandas library in 2008. Data can be imported into Pandas from a variety of file types, including JSON, SQL, Microsoft Excel, and others. The dataframes functionality is used to load data and manipulate it.

To perform analysis more effectively, we may need to restructure the Pandas data frame. In data analysis, reshaping is critical. Pandas offer reshaping functions such as melt() and unmelt().

Pandas melt() Function

Pandas melt: The melt() function in Pandas is used to convert the DataFrame format from wide to long. It is used to generate a special DataFrame object structure in which one or more columns serve as Identifiers. The remaining columns are all handled as values and are unpivoted to the row axis, leaving only two columns: variable and value.

Syntax:

pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value')

Parameters

frame: It is the actual dataframe to be melted.
id_vars: Names of columns that will be used as identifiers.
value_vars: All variable names that will be transformed into values (other than the identifier variables).
value_name: The name of the column value, which is the default.

Example

Approach:

  • Import pandas module using the import keyword.
  • Give some random data in JSON format and store it in a variable.
  • Pass the above data to pandas.DataFrame() function to convert it into a dataframe.
  • Print the above data frame before melting.
  • Pass the dataframe, Rollno as id_vars and “Names”, “Marks” as value_vars as the argument to the melt() function and to return the dataframe from wide to a longer format.
  • Print the above data frame after melting.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random data in Json format and store it in a variable
gvn_data = {"Names": ["Nick", "July", "Alex","Jhon"], "Rollno": [1, 2, 3, 4], "Marks": [75, 54, 90, 86]}
# Pass the above data to pandas.DataFrame() function to convert it into a dataframe
rslt_datafrme = pd.DataFrame(gvn_data)
# Print the above data frame before melting
print("The above data before melting:")
print(rslt_datafrme)
print()
# Pass the dataframe, Rollno as id_vars and "Names", "Marks" as value_vars as the argument to the
# melt() function and to return the dataframe from wide to a longer format
datafrme_meltd = pd.melt(rslt_datafrme, id_vars=["Rollno"], value_vars=["Names", "Marks"])
# Print the above data frame after melting
print("The above data after melting:")
print(datafrme_meltd)

Output:

The above data before melting:
  Names  Rollno  Marks
0  Nick       1     75
1  July       2     54
2  Alex       3     90
3  Jhon       4     86

The above data after melting:
   Rollno variable value
0       1    Names  Nick
1       2    Names  July
2       3    Names  Alex
3       4    Names  Jhon
4       1    Marks    75
5       2    Marks    54
6       3    Marks    90
7       4    Marks    86

Pandas unmelt() Function

Pandas.melt: The pivot() function can be used to unmelt a DataFrame object and retrieve the original dataframe. The ‘index’ argument value of the pivot() function should be the same as the ‘id vars’ value. The value of ‘columns’ should be used as the name of the ‘variable’ column.

Syntax:

pandas.pivot(index, columns)

Parameters

index: Labels that must be applied in order for the new data frame’s index in place.
columns: Labels that must be applied in order for the new data frame’s columns in place.

Example

Approach:

  • Import pandas module using the import keyword.
  • Give some random data in JSON format and store it in a variable.
  • Pass the above data to pandas.DataFrame() function to convert it into a dataframe and store it in another variable.
  • Print the above data frame before melting.
  • Pass the dataframe, Rollno as id_vars and “Names”, “Marks” as value_vars, var_name=”Expression”, value_name=”Value” that represets the unpivoted variables as the arguments to the melt() function to return the dataframe from wide to a longer format.
  • Print the above data frame after melting.
  • Apply pivot() function on the above-melted dataframe by passing index as ‘Rollno’ and columns as ‘Expression’ as the arguments to it.
  • Store it in another variable.
  • Print the above data after unmelting.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random data in Json format and store it in a variable
gvn_data = {"Names": ["Nick", "July", "Alex","Jhon"], "Rollno": [1, 2, 3, 4], "Marks": [75, 54, 90, 86]}
# Pass the avove data to pandas.DataFrame() function to convert it into a dataframe
rslt_datafrme = pd.DataFrame(gvn_data)
# Print the above data frame before melting
print("The above data before melting:")
print(rslt_datafrme)
print()
# Pass the dataframe, Rollno as id_vars and "Names", "Marks" as value_vars, var_name="Expression", 
# value_name="Value" that represets the unpivoted variables as the arguments to the melt() function
# to return the dataframe from wide to a longer format
datafrme_meltd = pd.melt(rslt_datafrme, id_vars=["Rollno"], value_vars=["Names", "Marks"], var_name="Expression", value_name="Value")
# Print the above data frame after melting
print("The above data after melting:")
print(datafrme_meltd)
print()
# Apply pivot() function on the above melted dataframe by passing index as 'Rollno'
# and columns as 'Expression' as the arguments to it.
# Store it in another variable.
datafrme_unmeltd = datafrme_meltd.pivot(index='Rollno', columns='Expression')
# Print the above data after unmelting
print("The above data after unmelting:")
print(datafrme_unmeltd)

Output:

The above data before melting:
  Names  Rollno  Marks
0  Nick       1     75
1  July       2     54
2  Alex       3     90
3  Jhon       4     86

The above data after melting:
   Rollno Expression Value
0       1      Names  Nick
1       2      Names  July
2       3      Names  Alex
3       4      Names  Jhon
4       1      Marks    75
5       2      Marks    54
6       3      Marks    90
7       4      Marks    86

The above data after unmelting:
           Value      
Expression Marks Names
Rollno                
1             75  Nick
2             54  July
3             90  Alex
4             86  Jhon

Refer More:

DIXON Pivot Point Calculator