Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({'Animal': ['Eagle', 'Eagle',
'Sparrow','Sparrow'],
'Max Speed': [350., 320., 26., 30.]})
df
df.groupby(['Animal']).mean()
Hierarchical Indexes
We can groupby different levels of a hierarchical index using the level parameter:
arrays = [['Eagle', 'Eagle', 'Sparrow', 'Sparrow'],
['Captive', 'Wild', 'Captive', 'Wild']]
index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
df = pd.DataFrame({'Max Speed': [370., 320., 30., 24.]},
index=index)
df
df.groupby(level=0).mean()
df.groupby(level=1).mean()