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
intorfloat, all other columns are automatically dropped.
pets
| Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
|---|---|---|---|---|---|---|---|
| 0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
| 1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
| 2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
| 3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
| 4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
| 5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
| 6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
| 7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! |
# Most cases, this won't be very helpful!
pets.groupby('Species').sum()
| Index | ID | Color | Weight | Age | Is_Cat | Owner_Comment |
|---|---|---|---|---|---|---|
| cat | cat_001cat_002cat_003 | goldenblackblack | 26.5 | 9.2 | 3 | My best birthday present ever!!!****All you need is love and a cat.****No, thank you! |
| dog | dog_001dog_002dog_003 | blackwhiteblack | 145 | 7.5 | 0 | 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. |
| hamster | ham_001ham_002 | blackgolden | 1.25 | 3.2 | 0 | No, thank you!No, thank you! |
pets.get(['Weight', 'Age', 'Species', 'Is_Cat']).groupby('Species').sum()
| Index | Weight | Age | Is_Cat |
|---|---|---|---|
| cat | 26.5 | 9.2 | 3 |
| dog | 145 | 7.5 | 0 |
| hamster | 1.25 | 3.2 | 0 |
Problems or suggestions about this page? Fill out our feedback form.