Understanding Pandas Melt — pd.melt()
Original Source Here
Before diving deeper into it, let us first create a wide DataFrame using pd.DataFrame()
.
A. Creating a wide DataFrame:
B. Example — 1:
Parameters Used:
No parameters used
If we do not specify any parameters while using the pd.melt()
method, it will melt all the columns with its’ corresponding values.
Python Implementation:
C. Example — 2:
Parameters Used:
id_vars: Person, House
In this example, we are going to melt our data_wide
DataFrame by specifying only theid_vars
parameter. The columns used in id_vars
(Person, House) are called identifier variables, and these columns will not be melted. Since we are not specifying any other parameters, all the other columns (Age, Books, Movies) will be melted to form a single column.
In the output, we have two new columns: variable and value. The variable column stores the names of the melted columns, and the value column stores the actual values of the melted columns. We can change the output column names using var_name
and value_name
parameters.
The critical thing to notice here is that not specifying which columns to melt will melt all the leftover columns after specifying the column names in id_vars
.
Python Implementation:
D. Example — 3:
Parameters Used:
id_vars: Person, House
value_vars: Age, Books, Movies
In this example, we are specifying id_vars
to specify the column names we do not want to melt (Person, House). Other than that, we are using the value_vars
parameter to specify the column names we want to melt (Age, Books, Movies).
Python Implementation:
E. Example — 4:
Parameters Used:
id_vars: Person
value_vars: Books, Movies
The following example shows that we can use as many columns as we want for the parameters id_vars
and value_vars.
Python Implementation:
AI/ML
Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot
via WordPress https://ramseyelbasheer.io/2021/04/22/understanding-pandas-melt%e2%80%8a-%e2%80%8apd-melt/