How to obtain the mean value over a specified axis in a DataFrame
Overview
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.
Syntax
The mean() function has the following syntax:
DataFrame.mean(axis=NoDefault.no_default, skipna=True, numeric_only=None, **kwargs)
Syntax for the mean() function in pandas
Parameters
The mean() function takes the following optional parameter values:
axis: This represents the name of the row ( designated as0or'index') or the column (designated as1orcolumns) 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.
Example
# 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"))
Explanation
- Line 4: We import the
pandaslibrary. - Lines 7–10: We create the
dfDataFrame. - Line 12: We print the
dfDataFrame. - Line 15: Using the
mean()function, we obtain the mean of the values running downwards across the rows (axis0). We print the result to the console. - Line 18: Using the
mean()function, we obtain the mean of the values running horizontally across columns (axis1). We print the result to the console.