Let's say that I have the following dataframe:
index K1 K2 D1 D2 D3
N1 0 1 12 4 6
N2 1 1 10 2 7
N3 0 0 3 5 8
Basically, I want to transform this dataframe into the following:
index COL1 COL2
K1 D1 = 0*12+1*10+0*3
K1 D2 = 0*4+1*2+0*5
K1 D3 = 0*6+1*7+0*8
K2 D1 = 1*12+1*10+0*3
K2 D2 = 1*4+1*2+0*5
K2 D3 = 1*6+1*7+0*8
The content of COL2
is basically the dot product (aka the scalar product) between the vector in index
and the one in COL1
. For example, let's take the first line of the resulting df. Under index
, we have K1
and, under COL1
we have D1
. Looking at the first table, we know that K1 = [0,1,0]
and D1 = [12,10,3]
. The scalar product of these two "vectors" is the value inside COL2
(first line).
I'm trying to find a way of doing this without using a nested loop (because the idea is to make something efficient), however, I don't exactly know how. I tried using the pd.melt()
function and, although it gets me closer to what I want, it doesn't exactly get me to where I want. Could you give me a hint?