.mean()
Published Jun 11, 2022
Contribute to Docs
The .mean()
method produces a new Series
or DataFrame
with aggregate mean values for the groups in a GroupBy
object.
Syntax
groupbyobject.mean(numeric_only)
numeric_only
is a boolean value. True
includes only int, float, and boolean columns. It defaults to True
.
Example
The following example produces a GroupBy
object from a DataFrame
and executes the .mean()
method on it.
import pandas as pddf = pd.DataFrame({'Key' : ['A', 'A', 'A', 'B', 'B', 'C'],'Value' : [15., 23., 17., 5., 8., 12.]})print(df, end='\n\n')group = df.groupby(['Key'], as_index=False)print(group.mean())
This example produces the following output:
Key Value0 A 15.01 A 23.02 A 17.03 B 5.04 B 8.05 C 12.0Key Value0 A 18.3333331 B 6.5000002 C 12.000000
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours