使用匹配的ID将值从一个数据框填充到另一个数据框 [英] Fill values from one dataframe to another with matching IDs

查看:55
本文介绍了使用匹配的ID将值从一个数据框填充到另一个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个熊猫数据帧,我想获得DF1中每个ID的items_bought的总和.然后在DF2中添加一列,其中包含从DF1计算出的具有相匹配ID的items_bought的总和,否则将其填充为0.如何以一种优雅而有效的方式做到这一点?

I have two pandas data frames, I want to get the sum of items_bought for each ID in DF1. Then add a column to DF2 containing the sum of items_bought calculated from DF1 with matching ID else fill it with 0. How can I do this in an elegant and efficient manner?

DF1

ID | items_bought
1        5
3        8
2        2
3        5
4        6
2        2

DF2

ID
1
2
8
3
2

期望的结果:DF2成为

Desired Result: DF2 Becomes

ID | items_bought
1        5
2        4
8        0
3        13
2        4

推荐答案

df1.groupby('ID').sum().loc[df2.ID].fillna(0).astype(int)
Out[104]: 
    items_bought
ID              
1              5
2              4
8              0
3             13
2              4

  1. 使用df1计算每个ID的总和.
  2. 现在,结果数据帧已由ID索引,因此您可以通过调用loc使用df2 ID进行选择.
  3. fillna填补空白.
  4. NA由浮点类型处理.现在已将它们删除,将列转换回整数.
  1. Work on df1 to calculate the sum for each ID.
  2. The resulting dataframe is now indexed by ID, so you can select with df2 IDs by calling loc.
  3. Fill the gaps with fillna.
  4. NA are handled by float type. Now that they are removed, convert the column back to integer.

这篇关于使用匹配的ID将值从一个数据框填充到另一个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆