[英]Multiply Column of One Dataframe by a Value from Another Dataframe, Determined by a Key
I want to take a large dataframe with around 26,000 rows, foodList, and multiply the column foodList['food_quant'] by a certain value from the dataframe foodConversions.我想采用大约 26,000 行的大型 dataframe,foodList,并将列 foodList['food_quant'] 乘以 dataframe foodConversions 中的某个值。 To determine this value from foodConversions, another column foodList['food_name'] has a string which corresponds to the index of foodConversions.
为了从 foodConversions 中确定这个值,另一列 foodList['food_name'] 有一个对应于 foodConversions 索引的字符串。 I am doing this to convert grams of different foods to calories, and each food type has a different number of calories.
我这样做是为了将不同食物的克数转换为卡路里,每种食物类型都有不同数量的卡路里。
I've tried doing nested loops to go through every value in foodConversions and see if it is equal to foodList['food_name'], but that's super slow and never actually finishes running for some reason;我尝试通过 foodConversions 中的每个值对 go 进行嵌套循环,看看它是否等于 foodList['food_name'],但这非常慢,并且由于某种原因实际上从未完成运行; hence, I would prefer to move away from this method.
因此,我宁愿放弃这种方法。 I have also tried using applymap and a lambda function, but I don't think I've done this right.
我也尝试过使用 applymap 和 lambda function,但我认为我做得不对。 Lastly, I've tried to use the methods outlined in another stackoverflow problem, but I wasn't sure how to apply it to my situation or if it even works for my situation.
最后,我尝试使用另一个 stackoverflow 问题中概述的方法,但我不确定如何将其应用于我的情况,或者它是否适用于我的情况。 Here's the link to it: Multiply dataframe with values from other dataframe
这是它的链接: 将 dataframe 与其他 dataframe 的值相乘
Here are the two dataframes:这是两个数据框:
foodConversions = pd.Dataframe([2,3], index=['meat','vegetables'], columns=['cal/gram'])
cal/gram
meat 2
vegetables 3
foodList = pd.Dataframe([['meat',40]['meat',30]['vegetables',20]['meat',10]], columns=['food_name','food_quant'])
food_name food_quant
0 meat 40
1 meat 30
2 vegetables 20
3 meat 10
And the output should look like: output 应该如下所示:
food_name food_quant
0 meat 80
1 meat 60
2 vegetables 60
3 meat 20
Hopefully that made sense, I tried to be as thorough as possible so I'm sorry for the lengthy explanation.希望这是有道理的,我试图尽可能彻底,所以我很抱歉冗长的解释。 Thanks everyone for you help!
谢谢大家的帮助!
We can do reindex
or loc
or map
ormerge
我们可以做
reindex
或loc
或map
或merge
reindex|loc
df2.assign(food_quant=df2.food_quant*(df1['cal/gram'].reindex(df2.food_name).values))# change reindex to loc
Out[121]:
food_name food_quant
0 meat 80
1 meat 60
2 vegetables 60
3 meat 20
map|replace
df2.assign(food_quant=df2.food_quant*df2.food_name.map(df1['cal/gram']))
df2.assign(food_quant=df2.food_quant*df2.food_name.replace(df1['cal/gram']))
Try using:尝试使用:
print(foodList.set_index('food_name').mul(foodConversions.reindex(foodList['food_name'])['cal/gram'], axis=0).reset_index())
Output: Output:
food_name food_quant
0 meat 80
1 meat 60
2 vegetables 60
3 meat 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.