2016-07-28 87 views
1

我有一個像通過分組和大熊貓添加值拼合列

id, index, name, count1, count2 
    1, 1, foo, 12, 10 
    1, 2, foo, 11, 12 
    1, 3, foo, 23, 12 
    1, 1, bar, 11, 21 
    ... 
    2, 1, foo, ... 

一個數據幀我希望得到一個數據幀如下

id, name, count1, count2 
1, foo, 46,34 
1, bar, .. 

所以基本上,我想「washaway」指數從這一領域..同時加入count1和count2列

我如何做到這一點在熊貓/ python?

回答

1

是你想要的嗎?

In [24]: df.groupby(['id','name']).sum().reset_index() 
Out[24]: 
    id name index count1 count2 
0 1 bar  1  11  21 
1 1 foo  6  46  34 

如果您想刪除index柱:

In [26]: df.groupby(['id','name']).sum().reset_index().drop('index', 1) 
Out[26]: 
    id name count1 count2 
0 1 bar  11  21 
1 1 foo  46  34 

數據:

In [25]: df 
Out[25]: 
    id index name count1 count2 
0 1  1 foo  12  10 
1 1  2 foo  11  12 
2 1  3 foo  23  12 
3 1  1 bar  11  21