Pandas按特定类别对行进行求和

时间:2016-02-28 13:04:08

标签: python pandas

这是我的疑问:
获取此数据框(从此question剪裁),例如:

   date     type        0            1              2            3             
2003-01-01 unemp  1.733275e+09  2.067889e+09  3.279421e+09  3.223396e+09   
2005-01-01 unemp  1.413758e+09  2.004171e+09  2.383106e+09  2.540857e+09   
2007-01-01 unemp  1.287548e+09  1.462072e+09  2.831217e+09  3.528558e+09   
2009-01-01 unemp  2.651480e+09  2.846055e+09  5.882084e+09  5.247459e+09   
2011-01-01 unemp  2.257016e+09  4.121532e+09  4.961291e+09  5.330930e+09   
2013-01-01 unemp  7.156784e+08  1.182770e+09  1.704251e+09  2.587171e+09   
2003-01-01 emp    6.012397e+09  9.692455e+09  2.288822e+10  3.215460e+10   
2005-01-01 emp    5.647393e+09  9.597211e+09  2.121828e+10  3.107219e+10   
2007-01-01 emp    4.617047e+09  8.030113e+09  2.005203e+10  2.755665e+10

我的目标:

总结具有不同类型(undmp / emp)的行,并创建一个新的数据帧:

http://i12.tietuku.com/49ae1e801254f460.png

2 个答案:

答案 0 :(得分:1)

使用groupby-sum

 >>> df.groupby('type').sum().reset_index()
    type    0   1   2   3
0   emp 16276837000 27319779000 64158530000 90783440000
1   unemp   10058755400 13684489000 21041370000 22458371000

答案 1 :(得分:1)

您可以使用groupby尝试sum

print df.groupby('type').sum()
                 0            1            2            3
type                                                     
emp    16276837000  27319779000  64158530000  90783440000
unemp  10058755400  13684489000  21041370000  22458371000

或者:

print df.groupby('type', as_index=False).sum()
    type            0            1            2            3
0    emp  16276837000  27319779000  64158530000  90783440000
1  unemp  10058755400  13684489000  21041370000  22458371000