PYTHON PANDAS DATAFRAME RESHAPING

Reshape pandas dataframe with melt in Python — tutorial and visualization

Convert wide to long with pd.melt

Hause Lin
Towards Data Science
5 min readMay 16, 2020

--

Reshape pandas dataframe with melt
How to use pd.melt() to reshape pandas dataframes from wide to long in Python (run code here)

There are many different ways to reshape a pandas dataframe from wide to long form. But the melt() method is the most flexible and probably the only one you need to use once you learn it well, just like how you only need to learn one method pivot_table() to reshape from long to wide (see my other post below).

This tutorial will walk you through reshaping dataframes using pd.melt() or the melt method associated with pandas dataframes. In other languages like R, melt is also known as gather. Also, R also has a melt function that works in the same way.

--

--

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Written by Hause Lin

researcher | like to learn, think, discuss ideas, combine data & behavioral science, classical music | hauselin.com | linkedin: t.ly/Ybvy

Responses (6)