0

Lets imagine a pandas dataframe table like (in sql the keys would be [type, product]):

type product ... a1 a2 a3 a4
0 0 ... a b c d
0 1 ... e f g h
0 2 ... i j k l
0 ... ... m n o p
0 n ... q r s t
1 0 ... ... ... ... ...
1 1 ... ... ... ... ...
1 2 ... ... ... ... ...
1 ... ... ... ... ... ...
1 n ... ... ... ... ...
2 0 ... ... ... ... ...
2 1 ... ... ... ... ...
2 2 ... ... ... ... ...
2 ... ... ... ... ... ...
2 n ... ... x y z

what I need as output is :

type product a index a value
0 0 a1 a
0 0 a2 b
0 0 a3 c
0 0 a4 d
0 1 a1 e
0 1 a2 f
0 1 a3 g
0 1 a4 h
... ... .... ....
2 n a4 z

So, transposing the rows from a1-a4 for every row and combining it with the keys.

Is this possible and if how.

Thanks

user1911091
  • 1,219
  • 2
  • 14
  • 32
  • i do not see how melt should help here. What to put for var_name value_name! – user1911091 Jun 02 '23 at 12:25
  • Actually, this is a melt, for var_name = 'a value' and for var_name = 'a index'. the id_vars of melt will be all columns except for a1, a2, a3, and a4. – Scott Boston Jun 02 '23 at 13:22

0 Answers0