Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas condense dataframe by summing according to ID

# Original dataframe df:
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

df.groupby(['Fruit','Name']).sum() # Use .groupby().sum()

#Output:                
Fruit   Name    Number     
Apples  Bob        16
        Mike        9
        Steve      10
Grapes  Bob        35
        Tom        87
        Tony       15
Oranges Bob        67
        Mike       57
        Tom        15
        Tony        1
Comment

PREVIOUS NEXT
Code Example
Python :: what is comma in regex 
Python :: get attribute of timestamp python 
Python :: vehari weather 
Python :: equivalent of case_when in r in pandas 
Python :: dfs and bfs inn python 
Python :: dip programming language 
Python :: python fibonacci while loop 
Python :: python fibbonacci 
Python :: copy any files from one folder to another folder in python 
Python :: pandas check if column type is list 
Python :: differentate derivative differentation 
Python :: hello kitt 
Python :: IntersectAll dynamo revit 
Python :: python cat binary files together 
Python :: how save second sheet in excel using python 
Python :: python paragraph Pypi 
Python :: pdb step into 
Python :: how to visualize pytorch model filters 
Python :: how to insert ele in python 
Python :: compute difference in dates after groupby 
Python :: space separated dictionary input in python 
Python :: # print random number 
Python :: pyton 
Python :: to create an array with values that are spaced linearly in a specified interval 
Python :: how to convert matlab code into python 
Python :: Math Module acos() Function in python 
Python :: install Social Auth App Flask 
Python :: plt hist random normal distribution 
Python :: odoo 13 vs code 
Python :: Broadcasting with NumPy Arrays Plotting a two-dimensional function Example 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =