Javatpoint Logo
Javatpoint Logo

Pandas melt()

The Pandas.melt() function is used to unpivot the DataFrame from a wide format to a long format.

Its main task is to massage a DataFrame into a format where some columns are identifier variables and remaining columns are considered as measured variables, are unpivoted to the row axis. It leaves just two non-identifier columns, variable and value.

Syntax

Parameters

  • frame: It refers to the DataFrame.
  • id_vars[tuple, list, or ndarray, optional]: It refers to the columns to use as identifier variables.
  • value_vars[tuple, list, or ndarray, optional]: Refers to columns to unpivot. If it is not specified, use all columns that are not set as id_vars.
  • var_name[scalar]: Refers to a name to use for the 'variable' column. If it is None, it uses frame.columns.name or 'variable'.
  • value_name[scalar, default 'value']: Refers to a name to use for the 'value' column.
  • col_level[int or string, optional]: It will use this level to melt if the columns are MultiIndex.

Returns

It returns the unpivoted DataFrame as the output.

Example

Output

      Name    Language     Age
0     Parker    Python     22
1     Smith     Java       30
2     John      C++        26

Example2

Output

       A    myVarname    myValname
0      p      C            56                
1      q      C            62                
2      r      C            42                

Next TopicDataFrame.merge()




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA