2.4 C
London
Friday, December 20, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Calculate the Mean by Group in Pandas (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories