In this tutorial, you will learn how to write a program to calculate mean value in pandas with example.
Pandas has inbuilt mean() function to calculate mean values. You can calculate for entire dataframe or single column also.
Syntax : DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Parameters:
axis : {index (0), columns (1)}
skipna : boolean, default True
Exclude NA/null values when computing the result.
level : int or level name, default None
If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only : boolean, default None
Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
import pandas as pd
data = pd.DataFrame({ 'name':['ravi','david','raju','david','kumar','teju'],
'experience':[1,2,3,4,5,2],
'salary':[15000,20000,30000,45389,50000,20000],
'join_year' :[2017,2017,2018,2018,2019,2018] })
#To calculate total mean
print(data.mean())
#to calculate mean for specific column
print(data['salary'].mean())
Output:
1 2 3 4 5 |
experience 2.833333 join_year 2017.833333 salary 30064.833333 dtype: float64 30064.833333333332 |