Pandas Melt β pd.melt()
Unpivot data in pandas using pd.melt
Pandas Melt is not only one of my favorite function names (makes me think of face melting in India Jones β gross clip), but itβs also a crucial data analysis tool.
Pandas pd.melt() will simply turn a wide table, tall. This will βunpivotβ your data so column(s) get enumerated into rows.
I use this function I want to turn pretty data thats easy to read (many columns) into usable data (many rows) that is easier to analyze.
In the above scenario, having two columns for dates (8/6 & 8/7) looks good, but itβs harder to do analysis on. I want turn those columns into row permutations.
Pseudo code: Take a column or columns, and transpose them into rows. βUnpivotβ your data.
Pandas Melt
Pandas Melt is a function youβll use when deciding the architecture of your of your data sets. This will ultimately lead to how you think about your analysis and questions you want to answer.
Melt Parameters
- id_vars: The column or columns youβd like to βunpivotβ around. This will be the column that will be expanded out.
- value_vars (Default: All non-id_vars columns): This is where you set which columns youβd like to unpivot. By default, everything other than id_vars will be unpivoted. You also have the option to specify a subset of columns.
- var_name: The name of your unpivoted column. This needs to be a single value (scalar).
- value_name: The name of your new values column.
- col_level: If you were using a MultiIndex, you specify which level you wanted to melt. This parameter wonβt get used much and is optional
- ignore_index: If you wanted to use your old index, no problem, set ignore_index=False. Iβd recommend leaving this default (True) and generating a new index.
Hereβs a Jupyter notebook showing how to Melt in Pandas