I have two data frames that I would like to combine based on certain conditions. This is the first data frame, each line represents one obversation (thus IDs occure multiple
I don't really know how efficient it is, but you can do that:
df1 = df1.set_index(['ID', 'Publication'])
df2 = df2.set_index('ID').stack()
df2.index.rename(['ID', 'Publication'], inplace=True)
df1['df2_value'] = df2
df1['result'] = df1['Count'] * df1['df2_value']
Using lookup
df2.set_index('ID').lookup(df1.ID,df1.Publication.astype(str))
Out[189]: array([1.1, 2.3, 1.1, 2.4, 1.2])
df1['Results']=df2.set_index('ID').lookup(df1.ID,df1.Publication.astype(str))*(df1.Count)
df1
Out[194]:
ID Count Publication Results
0 A 10 1990 11.0
1 B 15 1990 34.5
2 A 17 1990 18.7
3 B 19 1991 45.6
4 A 13 1991 15.6