如何对 pandas 中特定行的值求和? [英] How to sum values of particular rows in pandas?

查看:78
本文介绍了如何对 pandas 中特定行的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还不是很熟悉.我有一个看起来像这样的熊猫数据框:

I am not much familiar with Python yet. I have a pandas data frame that looks like this:

          0         1     2     3
55   Alice   12896399     8    45
45   Bob     16891982     0     0
90   Cybill   1800407     1     1
05   Alice   12896399   100   200
33   Bob     16891982   0.5     0
42   Bob     16891982  -1.5  -0.5
46   Bob     16891982     1     0
99   Cybill   1800407  0.00  0.00

我如何求和第2列和第3列的值,以得出每个人的结果?像这样:

How can i sum the values of columns 2 and 3 to get a result for each person? Like this:

   Alice     108    245
   Bob       0     -0.5
   Cybill    1     1

预先感谢您的答复.

推荐答案

IIUC,您可以在感兴趣的列上groupbysum:

IIUC you can groupby and sum on the cols of interest:

In [13]:
df.groupby('0')[['2','3']].sum()

Out[13]:
            2      3
0                   
Alice   108.0  245.0
Bob       0.0   -0.5
Cybill    1.0    1.0

这篇关于如何对 pandas 中特定行的值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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