• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / Pandas Melt: Reshape Wide Data to Long/Tidy Data

Pandas Melt: Reshape Wide Data to Long/Tidy Data

June 15, 2020 by cmdlinetips

Pandas offers multiple ways to reshape data in wide form to data in tidy or long form. Pandas melt() function is one of the powerful functions to use for reshaping dataframe with Python. In this case, we will see examples of basic use of Pandas melt to reshape wide data containing all numerical variables into tall data.

Let us load Pandas and NumPy. Let us also import poisson from scipy.stats.

import numpy as np
import pandas as pd
from scipy.stats import poisson

We will use scipy.stats to create random numbers from Poisson distribution. We will create three variables and use them to make a Pandas dataframe with wide data.

np.random.seed(seed=141)
c1= poisson.rvs(mu=10, size=3)
c2= poisson.rvs(mu=15, size=3)
c3= poisson.rvs(mu=20, size=3)

We use the three lists of random numbers to create Pandas dataframe in wide form.

df=pd.DataFrame({"C1":c1,
                 "C2":c2,
                 "C3":c3})

We have our data in wide form ready. Each column is a group name or variable name.

df

C1	C2	C3
0	15	19	22
1	12	15	13
2	14	16	24

Pandas Melt: A Simple Example

The simplest way to reshape wide data with all numerical columns to long/tidy form is to simply call melt() function on the dataframe.

In the example we use. And we get the reshaped data in long form with two columns. The first column is called “variable” by default and it contains the column/variable names. And the second column is named “value” and it contains the data from the wide form dataframe.

df.melt()

	variable	value
0	C1	15
1	C1	12
2	C1	14
3	C2	19
4	C2	15
5	C2	16
6	C3	22
7	C3	13
8	C3	24

Pandas Melt: Change Column Names

Let us see how to change the names of the columns of the tidy dataframe we get. To change the column name corresponding to variable name, we specify the argument “var_name” with the name we want.

df.melt(var_name=["Sample"]).head()

	Sample	value
0	C1	15
1	C1	12
2	C1	14
3	C2	19
4	C2	15

Similarly to specify a name for the values, we specify the argument “value_name” with the name we want.

df.melt(var_name="Sample", value_name="Count").head()

	Sample	Count
0	C1	15
1	C1	12
2	C1	14
3	C2	19
4	C2	15

This post is part of the series on Byte Size Pandas: Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Pandas Melt Example with IdentifiersPandas Melt: Reshape Wide to Tidy with identifiers Default ThumbnailHow to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()? Pandas melt to reshape dataframeHow To Reshape Pandas Dataframe with melt and wide_to_long()? Default Thumbnail5 Big Ideas Behind Tidy Evaluation

Filed Under: Pandas 101, Pandas Melt Tagged With: Python

Reader Interactions

Trackbacks

  1. Pandas Melt: Reshape Wide to Tidy with identifiers - Python and R Tips says:
    June 27, 2020 at 10:16 am

    […] melt() function is a versatile function to reshape Pandas dataframe. Earlier, we saw how to use Pandas melt() function to reshape a wide dataframe into long tidy dataframe, with a simple use case. Often while reshaping dataframe, you might want to reshape part of the […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version