0

I want to make two column header as row values comes under a new column and also make their values comes under another column header or column name using Python Pandas. I searched about it, but I could not found a solution for this.

First table: enter image description here

I want to make table like this :

second table: enter image description here

Can anyone give me a solution to solve this.

Abin Benny
  • 125
  • 2
  • 10
  • see [`pandas.melt`](https://pandas.pydata.org/docs/reference/api/pandas.melt.html) – I'mahdi Jul 24 '22 at 04:46
  • You should read [how to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – mozway Jul 24 '22 at 06:07

1 Answers1

2

Try this:

df1 = df.melt(id_vars = ['name', 'place'], value_vars = ['weight', 'numbers'], var_name = 'measure', value_name = 'measureing_values')

print(df1)



name  place  measure  measureing_values
0   apple  delhi   weight                  2
1  orange     up   weight                  3
2   onion    goa   weight                  4
3   apple  delhi  numbers                  6
4  orange     up  numbers                  8
5   onion    goa  numbers                 25
ragas
  • 848
  • 2
  • 7
  • How it can be write it as an MySQL Query, if the table name is `product`@ragas – Abin Benny Jul 24 '22 at 06:24
  • I don't have `mysql` with me. So, I can't run the code to test it before sharing that with you. But this tutorial will help you in pivoting. https://datacharmer.com/downloads/pivot_tables_mysql_5.pdf – ragas Jul 24 '22 at 14:58