查找所有ID的值,然后总和Python

发布于 2025-02-02 23:57:29 字数 740 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2025-02-09 23:57:29

您可以stackMAP分配 sum:ump:

out = (df1
 .stack()
 .map(df2.set_index('Student ID')['Grade'])
 .unstack()
 .assign(**{'Sum of Grades': lambda d: d.sum(axis=1)})
)

output:

   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          10.75

一个带有破碎步骤的替代方案:

s = df2.set_index('Student ID')['Grade']
out = df1.apply(lambda c: c.map(s))
out['Sum of Grades'] = out.sum(axis=1)

You can stack, map, and assign the sum:

out = (df1
 .stack()
 .map(df2.set_index('Student ID')['Grade'])
 .unstack()
 .assign(**{'Sum of Grades': lambda d: d.sum(axis=1)})
)

output:

   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          10.75

An alternative with broken steps:

s = df2.set_index('Student ID')['Grade']
out = df1.apply(lambda c: c.map(s))
out['Sum of Grades'] = out.sum(axis=1)
天涯离梦残月幽梦 2025-02-09 23:57:29

您可以使用 itertuples

df3 = df1.replace(dict(df2.set_index('Student ID')['Grade'].itertuples()))
df3['Sum of Grades'] = df3.sum(1)
 
   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            10.75

You could use itertuples:

df3 = df1.replace(dict(df2.set_index('Student ID')['Grade'].itertuples()))
df3['Sum of Grades'] = df3.sum(1)
 
   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            10.75
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文