Tsallaka zuwa abun ciki
Justice Tech School

Aggregate Several Ways

agg takes several functions or named aggregations, so you can compute many statistics in one step.

Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
r = df.groupby('city').agg(n=('name', 'count'), avg_score=('score', 'mean'), oldest=('age', 'max'))
print(r)

Result

         n  avg_score  oldest
city                         
Accra    1       92.0      25
Lagos    2       84.0      35
Nairobi  1       85.0      28

Exercise

Several statistics.

df.groupby("c").(["sum", "mean"])

Quiz Test

What are named aggregations?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.