[英]Calculate new column value based on values from other df
I have two data-frames. 我有两个数据帧。
First contains some info based on index like below:(First column is the index) 首先包含一些基于索引的信息,如下所示:(第一列是索引)
index,Dist,Individual Big,100,50 Small,50,100
second dataframe: 第二个数据帧:
id,hour,machinesize,type 1,10,Big,Dist 2,20,Small,Individual
I want to calculate the values like below: 我想计算如下的值:
hour* (values from df1 based on Big,Dist ) ie 10 *100 小时*(来自df1的值基于Big,Dist )即10 * 100
My final output will look like below: 我的最终输出如下所示:
id,hour,calc
1,10,1000
2,20,2000
The 'machinesize' value = 'index' in first df 第一个df中的'machinesize'值='index'
This will get you the desired result: 这将获得您想要的结果:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.