search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | melt method

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.melt(~) method converts the format of the source DataFrame from "wide" to "long".

Let's go through a quick example. Consider the following DataFrame:

name

age

height

alex

40

150

bob

50

160

This is considered to be a "wide" DataFrame since each row captures all relevant data about that person. Now, converting this to a "long" DataFrame:

name

variable

value

alex

age

30

alex

height

150

bob

age

50

bob

height

160

Now, each row captures a single variable about that person, which inevitably results in a vertically "long" DataFrame.

Pandas uses the term "unpivot" to denote the action of elongating the DataFrame based on a variable. In this example, we are unpivoting the variables age and height.

Parameters

1. id_varslink | tuple or list or Numpy array | optional

The label of the columns to be used as the identifier.

2. value_varslink | tuple or list or Numpy array | optional

The label of the columns to unpivot. By default, all columns will be unpivoted.

3. var_namelink | scalar | optional

The label of the variable column. By default, var_name="variable".

4. value_namelink | scalar | optional

The label of the value column. By default, value_name="value".

5. col_level | int or string | optional

The level to perform the method on. This is only relevant for columns that are multi-index.

Return Value

A DataFrame that has been unpivoted.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"name":["alex","bob","cathy"], "age":[40,50,60], "height":[150,160,170]})
df
   name   age  height
0  alex   40   150
1  bob    50   160
2  cathy  60   170

Basic usage

Here, the identifier column is name, and the columns to unpivot are age and height:

df.melt(id_vars="name", value_vars=["age","height"])
   name  variable  value
0  alex  age       40
1  bob    age       50
2  cathy age       60
3  alex  height    150
4  bob   height    160
5  cathy height    170

Here, we can actually omit the value_vars parameter since, by default, all columns except the identifier column (name) will be unpivoted.

Specifying value_vars

Suppose we wanted to unpivot just one column instead of two columns. We can simply specify which columns to unpivot using the value_vars parameter:

df.melt(id_vars="name", value_vars="age")
   name  variable  value
0  alex  age       40
1  bob    age       50
2  cathy age       60

Notice how the column height has been stripped away. If we want to keep the height column in there, we must include it in the id_vars parameter, like so:

df.melt(id_vars=["name","height"], value_vars="age")
   name  height  variable  value
0  alex  150     age       40
1  bob    160     age       50
2  cathy 170     age       60

Specifying var_name and value_name

Here, we show df again for your reference:

df
   name   age  height
0  alex   40   150
1  bob    50   160
2  cathy  60   170

As we've just seen in the examples above, the new column labels are "variable" and "value" by default, We can change this by specifying var_name and value_name, like so:

df.melt(id_vars="name", var_name="attribute", value_name="number")
   name   attribute  number
0  alex   age        40
1  bob    age        50
2  cathy  age        60
3  alex   height     150
4  bob    height     160
5  cathy  height     170
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!