pandas condense dataframe by summing according to id

Solutions on MaxInterview for pandas condense dataframe by summing according to id by the best coders in the world

showing results for - "pandas condense dataframe by summing according to id"
David
25 Jun 2020
1# Original dataframe df:
2Fruit   Date      Name  Number
3Apples  10/6/2016 Bob    7
4Apples  10/6/2016 Bob    8
5Apples  10/6/2016 Mike   9
6Apples  10/7/2016 Steve 10
7Apples  10/7/2016 Bob    1
8Oranges 10/7/2016 Bob    2
9Oranges 10/6/2016 Tom   15
10Oranges 10/6/2016 Mike  57
11Oranges 10/6/2016 Bob   65
12Oranges 10/7/2016 Tony   1
13Grapes  10/7/2016 Bob    1
14Grapes  10/7/2016 Tom   87
15Grapes  10/7/2016 Bob   22
16Grapes  10/7/2016 Bob   12
17Grapes  10/7/2016 Tony  15
18
19df.groupby(['Fruit','Name']).sum() # Use .groupby().sum()
20
21#Output:                
22Fruit   Name    Number     
23Apples  Bob        16
24        Mike        9
25        Steve      10
26Grapes  Bob        35
27        Tom        87
28        Tony       15
29Oranges Bob        67
30        Mike       57
31        Tom        15
32        Tony        1