Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas group-by and sum

I am using this data frame:

Fruit   Date      Name  Number Apples  10/6/2016 Bob    7 Apples  10/6/2016 Bob    8 Apples  10/6/2016 Mike   9 Apples  10/7/2016 Steve 10 Apples  10/7/2016 Bob    1 Oranges 10/7/2016 Bob    2 Oranges 10/6/2016 Tom   15 Oranges 10/6/2016 Mike  57 Oranges 10/6/2016 Bob   65 Oranges 10/7/2016 Tony   1 Grapes  10/7/2016 Bob    1 Grapes  10/7/2016 Tom   87 Grapes  10/7/2016 Bob   22 Grapes  10/7/2016 Bob   12 Grapes  10/7/2016 Tony  15 

I want to aggregate this by Name and then by fruit to get a total number of Fruit per Name. For example:

Bob,Apples,16 

I tried grouping by Name and Fruit but how do I get the total number of Fruit?

like image 473
Trying_hard Avatar asked Oct 07 '16 17:10

Trying_hard


People also ask

How do you group by and sum multiple columns in pandas?

Use DataFrame. groupby(). sum() to group rows based on one or multiple columns and calculate sum agg function. groupby() function returns a DataFrameGroupBy object which contains an aggregate function sum() to calculate a sum of a given column for each group.

Can you group by two things in pandas?

Pandas comes with a whole host of sql-like aggregation functions you can apply when grouping on one or more columns.


1 Answers

Use GroupBy.sum:

df.groupby(['Fruit','Name']).sum()  Out[31]:                 Number Fruit   Name          Apples  Bob        16         Mike        9         Steve      10 Grapes  Bob        35         Tom        87         Tony       15 Oranges Bob        67         Mike       57         Tom        15         Tony        1 
like image 103
Steven G Avatar answered Oct 01 '22 05:10

Steven G