Reputation: 2273
I have 2 dfs:
df1: 0 1
B B0 B1
S S0 S1
df2: B S
3 B3 S3
4 B4 S4
5 B5 S5
I am unsuccesfully trying to obtain new values for B and S which are a sumproduct of each row in df2 by its corresponding row in df1. The desired output im looking for is :
B S
3 B3*B0+S3*B1 B3*S0+S3*S1
4 B4*B0+S4*B1 B4*S0+S4*S1
5 B5*B0+S5*B1 B5*S0+S5*S1
The main problem im having is how to reference the corresponding indices (B or S) in df1 with the columns (B or S) in df2.
We consider that df2 has higher dimensions than df1
Upvotes: 1
Views: 1755
Reputation: 221514
That's basically matrix-multiplication between the second and transpose of the first -
df2.dot(df1.T)
Sample run -
1) Inputs :
In [100]: df1
Out[100]:
0 1
0 7 5
1 2 8
In [101]: df2
Out[101]:
0 1
0 6 5
1 1 5
2 6 6
2) Get outputs using expression posted in the question :
# First col of output using the expression used in the question
In [102]: df2.iloc[:,0]*df1.iloc[0,0] + df2.iloc[:,1]*df1.iloc[0,1]
Out[102]:
0 67
1 32
2 72
dtype: int64
# Second col of output using the expression used in the question
In [103]: df2.iloc[:,0]*df1.iloc[1,0] + df2.iloc[:,1]*df1.iloc[1,1]
Out[103]:
0 52
1 42
2 60
dtype: int64
3) Verify using proposed one :
# Using proposed method
In [104]: df2.dot(df1.T)
Out[104]:
0 1
0 67 52
1 32 42
2 72 60
Upvotes: 2