-1

I have a DataFrame that looks like this:

enter image description here

And I would like to transform it into something that looks like this

enter image description here

tedzz
  • 39
  • 1
  • 4

1 Answers1

2

You can switch from a wide to long format with melt. I've done this with a minimal example.

>>> df
  name  old_price  new_price
0    a        100        200
1    b        105        205

>>> df.melt(id_vars='name', var_name='price_type', value_name='price') 
  name price_type  price
0    a  old_price    100
1    b  old_price    105
2    a  new_price    200
3    b  new_price    205

The column given for id_vars is repeated for each "melted" row; the rest are "melted" into the var_name column and their respective value is put in the value_name column.

Steele Farnsworth
  • 863
  • 1
  • 6
  • 15