Row-wise lookup from other data frame

前端 未结 2 1096
陌清茗
陌清茗 2021-01-20 08:48

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

相关标签:
2条回答
  • 2021-01-20 09:43

    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']
    
    0 讨论(0)
  • 2021-01-20 09:49

    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
    
    0 讨论(0)
提交回复
热议问题