Skip to main content

sum()

df.groupby(column_name).sum()

Returns a DataFrame, with rows as groups, and each column as the sum of the column values within each group.

Note:
Only for columns of type int or float, all other columns are automatically dropped.

pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
2cat_002catblack159True****All you need is love and a cat.****
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!
# Most cases, this won't be very helpful!
pets.groupby('Species').sum()
IndexIDColorWeightAgeIs_CatOwner_Comment
catcat_001cat_002cat_003goldenblackblack26.59.23My best birthday present ever!!!****All you need is love and a cat.****No, thank you!
dogdog_001dog_002dog_003blackwhiteblack1457.50 There are no bad dogs, only bad owners.Love is a wet nose and a wagging tail.Be the person your dog thinks you are.
hamsterham_001ham_002blackgolden1.253.20No, thank you!No, thank you!
pets.get(['Weight', 'Age', 'Species', 'Is_Cat']).groupby('Species').sum()
IndexWeightAgeIs_Cat
cat26.59.23
dog1457.50
hamster1.253.20

See Also

Problems or suggestions about this page? Fill out our feedback form.