Pandas DataFrame: melt() function

DataFrame - melt() function

The melt() function is used to unpivot a given DataFrame from wide format to long format, optionally leaving identifier variables set.


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


Name Description Type/Default Value Required / Optional
frame      DataFrame Required
id_vars  Column(s) to use as identifier variables. tuple, list, or ndarray Optional
value_vars  Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars. tuple, list, or ndarray Optional
var_name  Name to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’. scalar Required
value_name  Name to use for the ‘value’ column. scalar
Default Value: ‘value’
col_level  If columns are a MultiIndex then use this level to melt. int or string Optional

Returns: DataFrame
Unpivoted DataFrame.


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - unstack() function
Next: DataFrame - explode() function

Follow us on Facebook and Twitter for latest update.