Reputation: 1371
I have two dataframes and I would like to use them to create a third by applying a simple function. The datasets are quite large so instead of looping through every row and column, is there a more efficient way to do this?
# dfA
id | value |
mars | 10 |
jupt | 15 |
satn | 14 |
# dfB
id | A | B | C |
satn | 0.5 | 0.3 | 0.2 |
mars | 0.2 | 0.5 | 0.3 |
jupt | 0.6 | 0.3 | 0.1 |
End result should be a multiplication of dfA
by the corresponding id
in dfB
# dfResult
id | A | B | C |
mars | 10*0.2 | 10*0.5 | 10*0.3 |
jupt | 15*0.5 | 15*0.3 | 15*0.1 |
satn | 14*0.5 | 14*0.3 | 14*0.2 |
Upvotes: 1
Views: 49
Reputation: 862406
Use DataFrame.set_index
for match, multiple by DataFrame.mul
and last create column from index
:
df = dfB.set_index('id').mul(dfA.set_index('id')['value'], axis=0).reset_index()
print (df)
id A B C
0 jupt 9.0 4.5 1.5
1 mars 2.0 5.0 3.0
2 satn 7.0 4.2 2.8
Upvotes: 3