Multiply two dataframes condition on another column
Use Series.map
for match by X1
and then multiple by X2
:
df1['X2'] *= df1['X1'].map(df2.set_index('X1')['X2'])
print (df1)
date X1 X2
0 01-01-2020 H 50
1 01-02-2020 H 150
2 01-03-2020 Y 150
3 01-04-2020 Y 200
Or use DataFrame.merge
with left join:
df1['X2'] *= df2.merge(df1, on='X1', how='left')['X2_x']
print (df1)
date X1 X2
0 01-01-2020 H 50
1 01-02-2020 H 150
2 01-03-2020 Y 150
3 01-04-2020 Y 200
You can set the index on both dataframes and assign the array to df
:
df["X2"] = df.set_index("X1").X2.mul(df1.set_index("X1").X2).array
df
date X1 X2
0 01-01-2020 H 50
1 01-02-2020 H 150
2 01-03-2020 Y 150
3 01-04-2020 Y 200
Use df.merge
and prod(axis=1)
df1['X2']=df1.merge(df2, how='left', on='X1').filter(like='_').prod(axis=1)
date X1 X2
0 01-01-2020 H 50
1 01-02-2020 H 150
2 01-03-2020 Y 150
3 01-04-2020 Y 200