Learn Before
Concept

Reshaping using Melt

The melt method allows for you to select certain columns of data and set them to be index columns which separate out the data. It will then convert the remaining items into variable - value relations.

first last height weight person A John Doe 5.5 130 B Mary Bo 6.0 150

Transforming the data using df.melt(id_vars = ["first", "last"])

first last variable value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0

The index values 0-3 can also be removed by setting the ignore_index = false.

0

1

Updated 2021-06-02

Tags

Python Programming Language

Data Science

Related