Dataframe.melt() in pandas
In this tutorial, we are going to learn how to use the melt() method in Pandas. The demonstration is done by using various examples. This is used to change the shape of the existing data frame. This is done for the process of data analysis. Here, the data frame appears in a long format rather than a wide format. So let’s begin the tutorial.
Parameters of dataframe.melt() in Pandas
id_vars: Columns that should be used in the final result.
value_vars: Columns used to unpivot. If no value is specified, all the columns of id_vars that are not set are considered.
var_name: The name that can be specified for the variable column.
value_name: The name that can be used to change the name of the value column.
col_level: This is used in the case of multi-level indexed columns.
Data Frame
The below data frame is created to demonstrate the melt() method.
import pandas as p data1 = { 'Age':[20,5,89,6], 'Gender':['M','M','F','M'], 'Qualified':['y','n','y','n'], 'Height':[5,3,6,3.2] } d1 = p.DataFrame(data1) print(d1)
OUTPUT:
Age Gender Qualified Height 0 20 M y 5.0 1 5 M n 3.0 2 89 F y 6.0 3 6 M n 3.2
Using melt() without any parameters
When melt() method is used without any parameters, all columns are melted to long form.
import pandas as p data1 = { 'Age':[20,5,89,6], 'Gender':['M','M','F','M'], 'Qualified':['y','n','y','n'], 'Height':[5,3,6,3.2] } d1 = p.DataFrame(data1) print(p.melt(d1))
OUTPUT:
variable value 0 Age 20 1 Age 5 2 Age 89 3 Age 6 4 Gender M 5 Gender M 6 Gender F 7 Gender M 8 Qualified y 9 Qualified n 10 Qualified y 11 Qualified n 12 Height 5 13 Height 3 14 Height 6 15 Height 3.2
Using id_vars parameter
Here the columns Age and Height are used as id_vars columns.
import pandas as p data1 = { 'Age':[20,5,89,6], 'Gender':['M','M','F','M'], 'Qualified':['y','n','y','n'], 'Height':[5,3,6,3.2] } d1 = p.DataFrame(data1) print(p.melt(d1, id_vars=['Age','Height']))
OUTPUT:
Age Height variable value 0 20 5.0 Gender M 1 5 3.0 Gender M 2 89 6.0 Gender F 3 6 3.2 Gender M 4 20 5.0 Qualified y 5 5 3.0 Qualified n 6 89 6.0 Qualified y 7 6 3.2 Qualified n
Using value_vars and var_name
Here, the values are displayed for the height column and the name of the column is changed as name.
import pandas as p data1 = { 'Age':[20,5,89,6], 'Gender':['M','M','F','M'], 'Qualified':['y','n','y','n'], 'Height':[5,3,6,3.2] } d1 = p.DataFrame(data1) print(p.melt(d1, id_vars=['Age'],value_vars=['Height'],var_name='Name'))
OUTPUT:
Age Name value 0 20 Height 5.0 1 5 Height 3.0 2 89 Height 6.0 3 6 Height 3.2
Using value_name
Here, we change the name of the values column.
import pandas as p data1 = { 'Age':[20,5,89,6], 'Gender':['M','M','F','M'], 'Qualified':['y','n','y','n'], 'Height':[5,3,6,3.2] } d1 = p.DataFrame(data1) print(p.melt(d1, id_vars=['Age'],value_vars=['Height'],var_name='Name',value_name='Height1'))
OUTPUT:
Age Name Height1 0 20 Height 5.0 1 5 Height 3.0 2 89 Height 6.0 3 6 Height 3.2
Also read: Dataframe.describe() in Pandas
Leave a Reply