Melt#

# general syntax
DataFrame.melt(self, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)

Parameters:

  • id_vars optional

    • Identifier variable column(s)

  • value_vars optional

    • Column(s) to unpivot; if not specified, uses all columns that aren’t id_vars

  • var_name required

    • Column name for the new variable column; if not specified, uses variable

  • value_name required

    • Column name for the new value column

  • col_level optional

    • Optional parameter to deal with multi-level index/multi-index

Let’s start with a simple DataFrame.

import numpy as np, pandas as pd # import statements

# create df
df = pd.DataFrame({'P': {0: 'p', 1: 'q', 2: 'r'},
                   'Q': {0: 1, 1: 3, 2: 5},
                   'R': {0: 2, 1: 4, 2: 6}})
df # show df

Example #1#

For our first .melt() example…

  • P column is the identifier

  • Q is the un-pivoted/melted (becomes a new column

  • R column is dropped

df.melt(id_vars=['P'], value_vars=['Q']) # melt example
dfMelt = df.melt(id_vars=['P'], value_vars=['Q']) # assign to new df
dfMelt # show output

We can also use the var_name and value_name parameters to customize the column names as part of the .melt() function.

# melt syntax that specifies column names
df.melt(id_vars=['P'], value_vars=['Q'],
        var_name='myVarname', value_name='myValname')

Example #2#

In our second .melt() example…

  • P is still the identifier

  • Q & R are the unpivoted/melted columns

df.melt(id_vars=['P'], value_vars=['Q', 'R']) # another melt example
dfMelt2 = df.melt(id_vars=['P'], value_vars=['Q', 'R']) # assign to new df
dfMelt2 # show output

Additional Resources#