The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

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.

1. pd.melt(Your_DateFrame)
2. df.melt()

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

Link to code

Official Documentation

On this page