How to Calculate Mean by Group of Dataframe in Python
To calculate the mean by group of a dataframe in Python, you can use the groupby() and mean() functions.
Method: Use groupby() and mean() Functions
dataframe.groupby('group')['column1'].mean()
The following example shows how to calculate the mean by group of a dataframe in Python.
Using groupby() & mean() Functions
We can use the groupby() and mean() functions to calculate the mean by group.
Suppose we have the following dataframe:
# Import pandas library
import pandas as pd
# Create a dataframe
office_stuff = pd.DataFrame({
'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
'Product_Name': ['Laptop', 'Mobile', 'Laptop', 'Keyboard', 'Mobile', 'Mouse'],
'Price': [4500, 550, 250, 50, 350, 50],
'Status': [1, 0, 1, 0, 1, 1]
})
# Calculate mean of Price based on Product_Name
mean_by_product_name = office_stuff.groupby('Product_Name')['Price'].mean()
# Show mean
print(mean_by_product_name)
Output: 👇️
Product_Name
Keyboard 50.0
Laptop 2375.0
Mobile 450.0
Mouse 50.0
Name: Price, dtype: float64
In this example, we use the groupby() function to group the dataframe by the Product_Name column and then calculate the mean of the Price column for each group using the mean() function.