查找所有ID的值,然后总和Python
我有2个带匹配ID的表。我想返回与表1中每个col关联的表2中的值并创建表3。我知道如何在Excel中使用Vlookup进行此操作。我也知道,在尝试执行诸如Vlookup之类的事情时,我应该使用加入或合并。但是,我不知道如何在这里获得所需的结果,因为我不能简单地将公式拖到Excel中的另一个单元格。
更新 如果我只能返回所需的总和而不是桌子和总和,这也对我有帮助。因此,表3仅是成绩的总和。
我用假数据构成了一个非常简单的示例。请在下面查看我所需的结果。
Table 1
Student 1 Student 2 Student 3
0 22882884 22882885 22882945
1 22882884 22882885 22882935
Table 2
Student ID Grade
0 22882884 4.0
1 22882885 3.5
2 22882945 2.75
3 22882935 3.25
Table 3
Student 1 Student 2 Student 3 Sum of Grades
0 4.0 3.5 2.75 10.25
1 4.0 3.5 3.25 9.75
I have 2 tables w/ matching ids. I want to return the value in table 2 associated with each col in table 1 and create table 3. I know how to do this in excel using vlookup. I also know that I should use join or merge when trying to do something like vlookup. However, I don't know how to get my desired result here because I can not simply drag the formula to another cell like in excel.
update
it would also be helpful to me if I could just return the desired sum rather than the table cols and the sum. So table 3 would just be sum of grades.
I made up a very simple example using fake data. Please see my desired result below.
Table 1
Student 1 Student 2 Student 3
0 22882884 22882885 22882945
1 22882884 22882885 22882935
Table 2
Student ID Grade
0 22882884 4.0
1 22882885 3.5
2 22882945 2.75
3 22882935 3.25
Table 3
Student 1 Student 2 Student 3 Sum of Grades
0 4.0 3.5 2.75 10.25
1 4.0 3.5 3.25 9.75
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以
stack
,MAP
和分配
sum:ump:output:
一个带有破碎步骤的替代方案:
You can
stack
,map
, andassign
the sum:output:
An alternative with broken steps:
您可以使用 itertuples :
You could use
itertuples
: