输入: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 Fairabc1 5 7abc2 6 9abc3 7 12
2 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
您可以使用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
偶然的你
TA贡献1841条经验 获得超3个赞
您可以map与字典查询一起使用
m = dict(df2.values)
df1.assign(**{'Increased Fair': df1.BusId.map(m) + df1['Fair Increase']})
BusId Fair Increase Increased Fair
0 abc1 2 7
1 abc2 3 9
2 abc3 5 12
添加回答
举报
0/150
提交
取消