Get Mean by Group in Python – Calculate Average in pandas DataFrame (2 Examples)

In this article, I’ll illustrate how to calculate the mean by group in Python.

Setting up the Examples

import pandas as pd                                # Import pandas library in Python
my_df = pd.DataFrame({'A':range(19, 28),           # Constructing a pandas DataFrame
                      'B':[6, 7, 3, 9, 1, 3, 8, 8, 9],
                      'C':range(20, 11, - 1),
                      'GRP_a':['gr1', 'gr1', 'gr2', 'gr3', 'gr1', 'gr2', 'gr2', 'gr3', 'gr3'],
                      'GRP_b':['a', 'b', 'c', 'c', 'a', 'b', 'b', 'a', 'a']})
print(my_df)
#     A  B   C GRP_a GRP_b
# 0  19  6  20   gr1     a
# 1  20  7  19   gr1     b
# 2  21  3  18   gr2     c
# 3  22  9  17   gr3     c
# 4  23  1  16   gr1     a
# 5  24  3  15   gr2     b
# 6  25  8  14   gr2     b
# 7  26  8  13   gr3     a
# 8  27  9  12   gr3     a

Example 1: Calculate Mean by Group

print(my_df.groupby('GRP_a').mean())               # Computing the column mean by group
#                A         B          C
# GRP_a                                
# gr1    20.666667  4.666667  18.333333
# gr2    23.333333  4.666667  15.666667
# gr3    25.000000  8.666667  14.000000

Example 2: Calculate Mean by Group & Subgroup

print(my_df.groupby(['GRP_a', 'GRP_b']).mean())    # Computing the column mean by multiple groups
#                 A    B     C
# GRP_a GRP_b                 
# gr1   a      21.0  3.5  18.0
#       b      20.0  7.0  19.0
# gr2   b      24.5  5.5  14.5
#       c      21.0  3.0  18.0
# gr3   a      26.5  8.5  12.5
#       c      22.0  9.0  17.0

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top