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 | Species | Color | Weight | Age |
|---|---|---|---|---|
| 0 | dog | black | 40 | 5 |
| 1 | cat | golden | 15 | 8 |
| 2 | cat | black | 20 | 9 |
| 3 | dog | white | 80 | 2 |
| 4 | dog | black | 25 | 0.5 |
| 5 | hamster | black | 1 | 3 |
| 6 | hamster | golden | 0.25 | 0.2 |
pets.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.