Pandas group and sum two columns

Beginner Question. It seems like this should be a simple operation, but I can't figure it out from reading the docs.

I have df with this structure:

|integer_id|int_field_1|int_field_2| 

The integer_id column is not unique, so I would like to group df by integer_id and sum the two fields.

Equivalent SQL:

 SELECT integer_id, SUM(int_field_1), SUM(int_field_2) FROM tbl GROUP BY integer_id 

Any suggestions on the easiest way to do this?

EDIT: enable I / O

 Input: integer_id int_field_1 int_field_2 2656 36 36 2656 36 36 9702 2 2 9702 1 1 

Ouput using df.groupby ('integer_id'). sum ():

 integer_id int_field_1 int_field_2 2656 72 72 9702 3 3 
+6
source share
2 answers

You just need to call sum on the groupby object:

 df.groupby('integer_id').sum() 

See docs for more details.

+10
source

You can do it

 data.groupby(by=['account_ID'])['purchases'].sum() 
+1
source

Source: https://habr.com/ru/post/974435/


All Articles