2016-07-28 5 views
1

Ich habe ein Datenrahmen wieSpalten Flatten durch die Gruppierung durch und das Hinzufügen von Werten in Pandas

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, ... 

ich einen Datenrahmen erhalten will, wie

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

Also im Grunde folgt, ich will „Washaway“ index aus diesem Feld .. während das Hinzufügen der Spalten count1 und count2

Wie mache ich das in Pandas/Python?

Antwort

1

ist das was du willst?

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 

wenn Sie wollen index Spalte löschen:

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 

Daten:

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 
Verwandte Themen