You can use Pandas groupby to group the underlying data on one or more columns and estimate useful statistics like count, sum, mean, median, min, max, etc. In this tutorial, we will look at how to get the mean for each group in pandas groupby with the help of some examples.
If you prefer video over text, check out the following video detailing the steps in this tutorial –

Pandas Groupby Mean
To get the average (or mean) value of in each group, you can directly apply the pandas mean()
function to the selected columns from the result of pandas groupby. The following is a step-by-step guide of what you need to do.
- Group the dataframe on the column(s) you want.
- Select the field(s) for which you want to estimate the mean.
- Apply the pandas
mean()
function directly or pass ‘mean’ to theagg()
function.
The following is the syntax –
# groupby columns Col1 and estimate the mean of column Col2 df.groupby([Col1])[Col2].mean() # alternatively, you can pass 'mean' to the agg() function df.groupby([Col1])[Col2].agg('mean')
Examples
Let’s now look at some examples of using the above syntax to get the average values for each group. First, we will create a sample dataframe that we will be using throughout this tutorial.
import pandas as pd # create a dataframe of car models by two companies df = pd.DataFrame({ 'Name': ['Rob', 'Rob', 'Rob', 'Emma', 'Emma', 'Emma', 'Hasan', 'Hasan', 'Hasan'], 'Subject': ['English', 'Science', 'Maths', 'English', 'Science', 'Maths', 'English', 'Science', 'Maths'], 'Marks': [67, 81, 59, 91, 80, 82, 73, 76, 54], 'Projects': [0, 1, 0, 1, 1, 0, 0, 2, 0] }) # display the dataframe df
Output:

Here we created a dataframe storing the marks obtained by some students in a high school in different subjects. The “Projects” field tells the number of projects submitted by the student in that subject.
Groupby Mean of a Single Column
Let’s get the average marks obtained by each student across all the subjects in the above dataframe. For this, we need to group the dataframe on “Name” and then estimate the mean of the “Marks” column.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# average marks for each student df.groupby('Name')['Marks'].mean()
Output:
Name Emma 84.333333 Hasan 67.666667 Rob 69.000000 Name: Marks, dtype: float64
You can see that we get the average marks for each student considering all the subjects. “Emma” has the highest average among the three students.
Alternatively, you can also use the agg()
function with “mean” as the argument to get the mean of each group in pandas groupby.
# average marks for each student df.groupby('Name')['Marks'].agg('mean')
Output:
Name Emma 84.333333 Hasan 67.666667 Rob 69.000000 Name: Marks, dtype: float64
We get the same result as above.
Groupby Mean of Multiple Columns
You can also get the mean of multiple columns at a time for each group. For example, let’s get the average number of projects submissions for each student along with their average marks.
# mean marks and project submissions for each student df.groupby('Name')[['Marks', 'Projects']].mean()
Output:

Here, we grouped the data on the “Name” column and then calculated the mean of values in the columns “Marks” and “Projects” for each group.
Alternatively, we can use the agg()
function with “mean” as the argument.
# mean marks and project submissions for each student df.groupby('Name')[['Marks', 'Projects']].agg('mean')
Output:

We get the same result as above.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.