Reputation: 295
Want to calculate multiplication. For that I want to convert row values as column for Df1
So Df1 matrix can be [6x1] and for Df2 [1x6]
Result = [1x6] * [6x1]
Df1:
Tech_ID s1 s2 s3 s4 s5 s6
-------------------------------
1 1 1 1 0 1 1
2 1 0 1 1 1 0
3 1 1 0 0 1 1
Df1 = {'Tech_ID':[1,2,3],
'S1':[1,1,1], 'S2':[1,0,1], 'S3': [1,1,0],
'S4':[0,1,0],'S5':[1,1,1],'S6':[1,0,1]}
Df2:
Tech_ID Std_Id s1_Norm s2 Norm S3 Norm S4_Norm S5 Norm S6_Norm
---------------------------------------------------------------------
1 1 0.87 0.87 0.27 0.83 0.37 0.83
1 7 0.25 0.45 0.45 0.28 0.15 0.22
2 4 0.38 0.38 0.58 0.31 0.28 0.32
3 8 0.44 0.24 0.64 0.40 0.44 0.48
2 2 0.73 0.43 0.33 0.78 0.53 0.75
Df2 = {'Tech_ID':[1,1,2,3,2], Std_Id [1,7,4,8,2]
'S1_norm':[0.87,0.25,0.38,0.44,0.73], 'S2_norm':[0.87,0.45,0.38,0.24,0.43],
'S3_norm': [0.27,0.45,0.58,0.64,0.33], 'S4_norm':[0.83,0.28,0.31,0.40,0.78],
'S5_norm': [0.37,0.15,0.28,0.44,0.53], 'S6_norm':[0.83,0.22,0.32,0.48,0.75],}
Calculation explanation,
[Df2] X [Df1]
_ _
| |
_ _ | S1 |
| | | S2 |
| s1_Norm s2 Norm S3 Norm S4_Norm S5 Norm S6_Norm | X | S3 |
|_ _| | S4 |
| S5 |
| S6 |
|_ _|
For Tech_ID: 1 And Std_Id: 1
_ _
| |
_ _ | 1 |
| | | 1 |
| 0.87 0.87 0.27 0.83 0.37 0.83 | X | 1 | = 3.21
|_ _| | 0 |
| 1 |
| 1 |
|_ _|
* Result= 3.21
* Final result = Result/ Length(count) of values = 3.21/6 = 0.535
Tech_ID Std_Id Result Final_Res
-----------------------------------
1 1 3.21 0.535
1 7 2.13 0.355
2 4 1.55 0.258
3 8 1.87 0.312
2 2 2.37 0.395
Confused about how to convert these theoretical concepts into a python script.
Upvotes: 1
Views: 52
Reputation: 150805
Try with:
df2['Result'] = (df1.set_index('Tech_ID')
.mul(df2.set_index('Tech_ID').rename(columns=lambda x: x.split('_')[0]))
.sum(1)
.to_numpy()
)
df2['Final_Res'] = df2['Result']/(df1.shape[1]-1)
print(df2[['Tech_ID','Std_Id','Result','Final_Res']])
Output:
Tech_ID Std_Id Result Final_Res
0 1 1 3.21 0.535000
1 1 7 1.52 0.253333
2 2 4 1.55 0.258333
3 3 8 2.37 0.395000
4 2 2 1.60 0.266667
Upvotes: 2