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:

Figure 4: Creating Main DataFrame.
Figure 5: Python Code.
Figure 6: Python Code Output.

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.

Figure 7: Main DataFrame.
Figure 8: Using pd.melt( ) Function.
Figure 9: Output.
Figure 10: Output.

Python Implementation:

Figure 11: Python Code.
Figure 12: Python Code Output.

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.

Figure 13: Main DataFrame.
Figure 14: Using pd.melt( ) Function.
Figure 15: Output.

Python Implementation:

Figure 16: Python Code.
Figure 17: Python Code Output.

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).

Figure 18: Main DataFrame.
Figure 19: Using pd.melt( ) Function.
Figure 20: Output.

Python Implementation:

Figure 21: Python Code.
Figure 22: Python Code Output.

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.

Figure 23: Main DataFrame.
Figure 24: Using pd.melt( ) Function.
Figure 25: Output.

Python Implementation:

Figure 26: Python Code.
Figure 27: Python Code Output.

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/

Popular posts from this blog

I’m Sorry! Evernote Has A New ‘Home’ Now

Jensen Huang: Racism is one flywheel we must stop

Fully Explained DBScan Clustering Algorithm with Python