pandas groupby mean and the closest to the mean code example Example: pandas groupby mean df.groupby(['A', 'B']).mean()