根据其他df的值计算新的列值

时间:2019-06-03 12:55:37

标签: python python-3.x pandas

我有两个数据框。
首先包含一些基于索引的信息,如下所示:(第一列是索引)

index,Dist,Individual
Big,100,50
Small,50,100

第二个数据帧:

id,hour,machinesize,type 
1,10,Big,Dist
2,20,Small,Individual

我想计算如下值:

小时*(df1中的值基于大,远) 即10 * 100
我的最终输出将如下所示:

id,hour,calc
1,10,1000
2,20,2000  

第一个df中的“ machinesize”值=“ index”

1 个答案:

答案 0 :(得分:0)

这将为您提供所需的结果:

result = df2.merge(df1, left_on='machinezise', right_on='index')

result.assign(calc=result.lookup(result.index, result.type)*result.hour)[['id', 'hour', 'calc']]

result

   id  hour  calc
0   1    10  1000
1   2    20  2000
相关问题