The mean()
function in pandas is used to obtain the mean of the values over a specified axis in a given DataFrame.
Mathematically, the mean can be defined as the sum of all values in a dataset divided by the number of values.
The mean()
function has the following syntax:
DataFrame.mean(axis=NoDefault.no_default, skipna=True, numeric_only=None, **kwargs)
The mean()
function takes the following optional parameter values:
axis
: This represents the name of the row ( designated as 0
or 'index'
) or the column (designated as 1
or columns
) axis from which to take the mean.skipna
: This takes a boolean value. It determines whether null values are to be excluded or not in the calculation of the mean.numeric_only
: This takes a boolean value. It determines whether only float, int, or boolean columns are included in the calculation.**kwargs
: This is an additional keyword argument that can be passed to the function.# A code to illustrate the mean() function in Pandas# importing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame([[1,2,3,4,5],[1,7,5,9,0.5],[3,11,13,14,12]],columns=list('ABCDE'))# printing the dataframeprint(df)# obtaining the mean value vertically across rowsprint("Mean across rows: ", df.mean())# obtaining the mean value horizontally over columnsprint("Mean across columns: ", df.mean(axis="columns"))
pandas
library.df
DataFrame.df
DataFrame.mean()
function, we obtain the mean of the values running downwards across the rows (axis 0
). We print the result to the console.mean()
function, we obtain the mean of the values running horizontally across columns (axis 1
). We print the result to the console.