Hey, folks! In this article, we will be focusing on Python mean() function in order to perform the mean operation.

**Mean **is the value that represents an entire set of entities. It is considered as the *central value* of a set of numbers.

Mean is calculated by dividing the summation of all the entity values by the number of entities. Essentially, the mean is the average of the specified values.

Formula:

`(sum of values)/total values`

Now, let us understand the working of** the mean() function** to calculate the mean.

Table of Contents

## Using the Python mean() function

The mean() function helps in calculating the mean of a set of values passed to the function.

Python statistics module is used to perform all the statistical operations on the data. We need to import the statistics module using the below command:

**Syntax**: Importing the statistics module

```
import statistics
```

The statistics.mean() function accepts the data values as argument and returns the mean of the values passed to it.

**Syntax:**

```
statistics.mean(data)
```

**Example:**

```
import statistics
data = [10,20,30,40,50]
res_mean = statistics.mean(data)
print(res_mean)
```

**Output:**

```
30
```

## Python mean() function with NumPy module

Python NumPy module represents the set of values in the form of an array. We can calculate the mean of these array elements using numpy.mean() function.

The `numpy.mean() function`

performs the same way as of the statistics.mean() function.

**Syntax:**

```
numpy.mean(data)
```

**Example:**

```
import numpy as np
data = np.arange(1,10)
res_mean = np.mean(data)
print(res_mean)
```

In the above example, we have used the `numpy.arange(start,stop)`

function to generate evenly spaced values in the range provided as parameters. Further, `numpy.mean() function`

is used to calculate the mean of all the array elements.

**Output:**

```
5.0
```

## Python mean() function with Pandas module

Python Pandas module deals with huge datasets in the form of DataFrames. The mean of those huge data sets can be calculated by using pandas.DataFrame.mean() function.

The `pandas.DataFrame.mean()`

function returns the mean of those data values.

**Syntax:**

```
pandas.DataFrame.mean()
```

**Example 1:**

```
import numpy as np
import pandas as pd
data = np.arange(1,10)
df = pd.DataFrame(data)
res_mean = df.mean()
print(res_mean)
```

In the above example, we have created a **NumPy array** using `numpy.arange() function`

and then converted the array values into a DataFrame using `pandas.DataFrame() function`

. Further, we have calculated the mean of the DataFrame values using `pandas.DataFrame.mean() function`

.

**Output:**

```
0 5.0
dtype: float64
```

**Example 2:**

```
import pandas as pd
data = pd.read_csv("C:/mtcars.csv")
res_mean = data['qsec'].mean()
print(res_mean)
```

**Input Dataset:**

In the above example, we have used the above mentioned dataset, and calculated the mean of all the data values present in the data column ‘**qsec**‘.

**Output:**

```
17.848750000000003
```

## Conclusion

Thus, in this article, we have understood the working of Python mean() function along with NumPy and Pandas module.

## References

Python mean() function — Official Documentation