How can I get df3
, that is a multiplication of the values in df1
by the coefficients in df2
matching on the type and year.
df1
:
year type_1 type_2 type_3 pepse
0 2011 1 1 1 1
1 2011 2 2 2 1
2 2011 3 3 3 1
3 2011 4 4 4 1
df2
:
year combo_type X Y
0 2011 type_1 1 5
1 2011 type_3 11 6
output df3
:
year type_1 type_2 type_3 pepse X_type_1 X_type_2 X_type_3 Y_type_1 Y_type_2 Y_type_3
0 2011 1 1 1 1 1 0 11 5 0 6
1 2011 2 2 2 1 1 0 11 5 0 6
2 2011 3 3 3 1 1 0 11 5 0 6
3 2011 4 4 4 1 1 0 11 5 0 6
dataframes:
df1 = pd.DataFrame({'year':[2011,2011,2011,2011],'type_1':[1,2,3,4],'type_2':[1,2,3,4],'type_3':[1,2,3,4],'pepse':[1,1,1,1]})
df2 = pd.DataFrame({'year':[2011,2011],'combo_type':['type_1','type_3',],'X':[1,11],'Y':[5,6,]})
df3 = pd.DataFrame({'year':[2011,2011,2011,2011],'type_1':[1,2,3,4],'type_2':[1,2,3,4],'type_3':[1,2,3,4],'pepse':[1,1,1,1],'X_type_1':[1,1,1,1],'X_type_2':[0,0,0,0],'X_type_3':[11,11,11,11],'Y_type_1':[5,5,5,5],'Y_type_2':[0,0,0,0],'Y_type_3':[6,6,6,6]})