输入:

df1=pd.DataFrame({
    "BusId":['abc1','abc2','abc3'],
    "Fair Increase":[2,3,5]
})
df2=pd.DataFrame({
    'BusId':['abc1','abc2','abc3','abc4','abc5'],
    "Fair":[5,6,7,8,9]
})


只需要计算df2上df1中存在的BusId。

计算增加的​​公平
df2公平+ df1公平增长

预期产量:

BusId   Fair    Increased Fair
abc1    5           7
abc2    6           9
abc3    7           12

最佳答案

您可以使用df.merge合并df2和df2,创建新列Increased Fair并删除旧列Fair Increase

>>> df3 = df2.merge(df1).set_index('BusId')
>>> df3['Increased Fair'] = df3['Fair'] + df3['Fair Increase']
>>> del df3['Fair Increase']
>>> df3
       Fair  Increased Fair
BusId
abc1      5               7
abc2      6               9
abc3      7              12

10-07 17:55