Tabularize columnar index data into row data
df.melt()
Sometimes you may have some columnar data that you want to transpose into row data, for that you can use melt to extact that columnar data from a dataframe
- It will repeat values from your column labels to match all it's row records
Some of it's arguments are as follow:
value_vars
: An array like of column names to melt (mandatory)ignore_index
: Ignore the original index of the dataframe (defaultTrue
)var_name
: Name to give to the melted columnid_vars
: Array like of columns from original dataframe to keep and repeat values
Unlike stacking this will not turn your column into part of the index, it will create it as a new column where it's values match the corresponing value from the orginal dataframe
df.melt(
id_vars=['sector'],
value_vars=df.columns.difference['sector']
var_name='date'
)