How to Calculate the Mean by Group in Pandas (With Examples)


You can use the following methods to calculate the mean value by group in pandas:

Method 1: Calculate Mean of One Column Grouped by One Column

df.groupby(['group_col'])['value_col'].mean()

Method 2: Calculate Mean of Multiple Columns Grouped by One Column

df.groupby(['group_col'])['value_col1', 'value_col2'].mean()

Method 3: Calculate Mean of One Column Grouped by Multiple Columns

df.groupby(['group_col1', 'group_col2'])['value_col'].mean()

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'F', 'F', 'G', 'F', 'F', 'G', 'G'],
                   'points': [30, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [4, 3, 7, 7, 12, 15, 8, 4]})

#view DataFrame
print(df)

  team position  points  assists
0    A        G      30        4
1    A        F      22        3
2    A        F      19        7
3    A        G      14        7
4    B        F      14       12
5    B        F      11       15
6    B        G      20        8
7    B        G      28        4

Example 1: Calculate Mean of One Column Grouped by One Column

The following code shows how to calculate the mean value of the points column, grouped by the team column:

#calculate mean of points grouped by team
df.groupby('team')['points'].mean()

team
A    21.25
B    18.25
Name: points, dtype: float64

From the output we can see:

  • The mean points value for team A is 21.25.
  • The mean points value for team B is 18.25.

Example 2: Calculate Mean of Multiple Columns Grouped by One Column

The following code shows how to calculate the mean value of the points column and the mean value of the assists column, grouped by the team column:

#calculate mean of points and mean of assists grouped by team
df.groupby('team')[['points', 'assists']].mean()

       points	assists
team		
A	21.25	   5.25
B	18.25	   9.75

The output displays the mean points value and mean assists value for each team.

Example 3: Calculate Mean of One Column Grouped by Multiple Columns

The following code shows how to calculate the mean value of the points column, grouped by the team and position columns:

#calculate mean of points, grouped by team and position
df.groupby(['team', 'position'])['points'].mean()

team  position
A     F           20.5
      G           22.0
B     F           12.5
      G           24.0
Name: points, dtype: float64

From the output we can see:

  • The mean points value for players on team A and position F is 20.5.
  • The mean points value for players on team A and position G is 22.
  • The mean points value for players on team B and position F is 12.5.
  • The mean points value for players on team B and position G is 24.

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

How to Find the Max Value by Group in Pandas
How to Find Sum by Group in Pandas
How to Calculate Quantiles by Group in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *