Pandas Essentials: Melt

A quick example demonstrating how to use pd.melt to reshape data

Eric Onofrey
2 min readAug 3, 2021
Photo by Heather Barnes on Unsplash

What

From the official pandas documentation, pd.melt “Unpivot(s) a DataFrame from wide to long format, optionally leaving identifiers set.”

Why

Melt is useful when one wants to convert several columns of data, all measuring distinct values, into 1 column with identifiers for each row. When gathering data, people typically record entries in “wide format”, with a subject in each row and various attributes about that subject in separate columns. To demonstrate this, I’ve manually collected ratings from IMDB, Rotten Tomato, and Metacritic on all of the Fast & Furious movies below.

Fast & Furious Data: Wide Format

While this format is easy for humans to interpret, often is the case where it’s easier to work with the data if it’s in “long form”. Each subject appears in multiple rows, with one row for each dimension (different movie ratings in this case). Below is an example of the same Fast & Furious rating data, but in long form with one column for all ratings, and two columns describing the movie and source of the rating.

--

--

Eric Onofrey
Eric Onofrey

No responses yet