![]() |
VOOZH | about |
To make the analysis of data in a table easier, we can reshape the data into a more computer-friendly form using Pandas in Python. Pandas.melt() is one of the functions to do so.. Pandas.melt() unpivots a DataFrame from wide format to long format.
Pandas melt() function is useful to massage a DataFrame into a format where one or more columns are identifier variables, while all other columns, considered measured variables, are unpivoted to the row axis, leaving just two non-identifier columns, variable and value.
Syntax: pandas.melt(frame, id_vars=None, value_vars=None,
var_name=None, value_name='value', col_level=None)
Parameters:
- frame :DataFrame
- id_vars[tuple, list, or ndarray, optional] : Column(s) to use as identifier variables.
- value_vars[tuple, list, or ndarray, optional]:Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
- var_name[scalar]: Name to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’.
- value_name[scalar, default ‘value’]:Name to use for the ‘value’ column.
- col_level[int or string, optional]:If columns are a MultiIndex then use this level to melt.
Here, we have created a sample DataFrame that we will use in this article.
👁 ImageBelow are the example of how we can use Pandas melt() Function in different ways in Pandas:
In this example, the pd.melt function is used to unpivot the 'Course' column while keeping 'Name' as the identifier variable. The resulting DataFrame has three columns: 'Name', 'variable' (containing the column name 'Course'), and 'value' (containing corresponding values from the 'Course' column).
Output:
Name variable value
0 John Course Masters
1 Bob Course Graduate
2 Shiela Course Graduate
In this example, the pd.melt function is employed to unpivot the 'Course' and 'Age' columns while using 'Name' as the identifier variable.
Output:
Name variable value
0 John Course Masters
1 Bob Course Graduate
2 Shiela Course Graduate
3 John Age 27
4 Bob Age 23
5 Shiela Age 21
In this example, the pd.melt function is utilized with customized column names. The 'Course' column is unpivoted while preserving 'Name' as the identifier. The resulting DataFrame has columns 'Name', 'ChangedVarname' (for the melted column name, set to 'Course'), and 'ChangedValname' (containing corresponding values from the 'Course' column).
Output:
Name ChangedVarname ChangedValname
0 John Course Masters
1 Bob Course Graduate
2 Shiela Course Graduate
In this example, the pd.melt function is applied to unpivot the 'Course' and 'Age' columns while using 'Name' as the identifier variable. The original index is ignored due to the ignore_index=True parameter.
Output:
Name variable value
0 John Course Masters
1 Bob Course Graduate
2 Shiela Course Graduate
3 John Age 27
4 Bob Age 23
5 Shiela Age 21