# 3 Ways to Calculate the Mean in Python

In this short guide, you’ll see how to calculate the mean in Python.

## 3 Ways to Calculate the Mean in Python

In the sections below, you’ll observe 3 ways to calculate the mean in Python. For each of the methods to be reviewed, the goal is to derive the mean, given the following values:

8, 20, 12, 15, 4

### Method 1: Simple Average Calculation

To start, you can use the following average calculations to derive the mean:

```sum_values = 8 + 20 + 12 + 15 + 4
n = 5
mean = sum_values/n
print ('The Mean is: ' + str(mean))
```

Where:

• sum_values represents the sum of all the values in the dataset
• n reflects the number of items in the dataset. In this case, there are 5 items
• mean = sum_values/n is the actual calculation to get the mean

When you run the code in Python, you’ll get the mean of 11.8

### Method 2: Using a List to get the Mean

Alternatively, you can use a list that contains the values to be averaged (here, you’ll noticed that the values are placed in a list, where each value is separated by a comma):

```sum = 0
values = [8,20,12,15,4]
n = len(values)

for i in values:
sum = sum + i

mean = sum/n
print ('The Mean is: ' + str(mean))
```

When you run the code, you’ll get the same mean of 11.8

### Method 3: Using Pandas to get the Mean in Python

You can also derive the mean using Pandas.

In our example, you’ll need to apply the syntax below to get the mean:

```from pandas import DataFrame

data = {'values': [8,20,12,15,4]}
df = DataFrame(data)

mean = df['values'].mean()
print ('The Mean is: ' + str(mean))
```

As before, you’ll get the mean of 11.8