How to calculate the mean over a dataframe column with pandas in python ?

How to calculate the mean over a dataframe column with pandas in python ?

Daidalos June 18, 2020


Examples of how to calculate the mean over a dataframe column with pandas in python:

1 -- Create a dataframe

Lets consider the following dataframe:

import pandas as pd

data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 
        'Age':[20,27,43,30,12,21]}

df = pd.DataFrame(data)

returns

    Name  Age
0    Ben   20
1   Anna   27
2    Zoe   43
3    Tom   30
4   John   12
5  Steve   21

2 -- Calculate the mean of age

To calculate the mean over the column called above 'Age' a solution is to use mean(), example

df['Age'].mean()

returns

25.5

3 -- Another example with a NaN value in the column

import pandas as pd
import numpy as np

data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Bob'], 
        'Age':[20,27,43,30,12,21, np.nan]}

df = pd.DataFrame(data)

    Name   Age
0    Ben  20.0
1   Anna  27.0
2    Zoe  43.0
3    Tom  30.0
4   John  12.0
5  Steve  21.0
6    Bob   NaN

df['Age'].mean()

returns

25.5

4 -- References