# 4 Ways to Calculate the Geometric Mean in Python

In this short guide, I’ll show you 4 ways to calculate the geometric mean in Python.

By the end of this guide, you’ll be able to derive the geometric mean using this simple program: ## 4 Ways to Calculate the Geometric Mean in Python

In the following section, you’ll see 4 methods to calculate the geometric mean in Python. For each of the methods to be reviewed, the goal is to derive the geometric mean, given the values below:

8, 16, 22, 12, 41

### Method 1: Simple Calculations to get the Geometric Mean

To start, you can use this simple calculation to get the geometric mean:

```multiplyValues = 8*16*22*12*41
n = 5
geometricMean = (multiplyValues)**(1/n)
print ('The Geometric Mean is: ' + str(geometricMean))
```

Where:

• multiplyValues represents the multiplication of all the values in the dataset
• reflects the number of items in the dataset. In our example, there are 5 items
• geometricMean = (multiplyValues)**(1/n) is the actual calculation to derive the geometric mean

Run the code in Python, and you’ll get the following result: ### Method 2: Using a List to Derive the Geometric Mean in Python

Alternatively, you can place all the values in a list, where each value should be separated by a comma:

```multiply = 1
values = [8,16,22,12,41]
n = len(values)

for i in values:
multiply = (multiply)*(i)

geometricMean = (multiply)**(1/n)
print ('The Geometric Mean is: ' + str(geometricMean))
```

Once you run the code in Python, you’ll get the same result: ### Method 3: Using Pandas and Scipy

You could also use Pandas and Scipy to obtain the geometric mean:

```from pandas import DataFrame
from scipy.stats.mstats import gmean

data = {'values': [8,16,22,12,41]}
df = DataFrame(data)

geometricMean = gmean(df.loc[:,'values'])
print ('The Geometric Mean is: ' + str(geometricMean))
```

As before, you’ll get the same geometric mean: ### Method 4: Get the Geometric Mean using Tkinter

Tkinter is a Python package that can be used to build a graphical user interface (GUI).

You can then use the following syntax to launch a simple GUI in order to derive the geometric mean:

```import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 500, height = 400)
canvas1.pack()

label0 = tk.Label(root, text='Calculate Geometric Mean')
label0.config(font=('helvetica', 14))
canvas1.create_window(250, 40, window=label0)

entry1 = tk.Entry (root, width = 30)
canvas1.create_window(350, 140, window=entry1)

label1 = tk.Label(root, text='Values (Separated by Commas):')
label1.config(font=('helvetica', 10))
canvas1.create_window(150, 140, window=label1)

def calcMean ():
values = entry1.get().split(',')
n = len(values)

multiply = 1

for i in values:
multiply = (multiply)*float(i)

geometricMean = (multiply)**(1/n)

label2 = tk.Label(root, text= geometricMean,font=('helvetica', 10, 'bold'),bg='white')
canvas1.create_window(250, 300, window=label2)

button1 = tk.Button(text='Calculate Geometric Mean', command=calcMean, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 25)
canvas1.create_window(250, 250, window=button1)

root.mainloop()
```

Run the above code and you’ll see this display: Simply enter the values in the input box. For our example, enter the following values:

8, 16, 22, 12, 41

Once you are ready, click on the green button to calculate the geometric mean: You’ll then get the same geometric mean as in the previous methods: 