Data Analysis with Python: Calculating Mean and Median

751
2
10-04-2023 12:14 AM
hectorsalamanca
New Contributor III

I'm working on a data analysis project in Python and need to calculate both the mean and median of a dataset. I understand the basic concepts, but I'm looking for a Python code example that demonstrates how to do this efficiently.

Let's say I have a list of numbers:

 

data = [12, 45, 67, 23, 41, 89, 34, 54, 21]

 

 

I want to calculate both the mean and median of these numbers. Could you provide a Python code snippet that accomplishes this? Additionally, it would be helpful if you could explain any libraries or functions used in the code.

Thank you for your assistance in calculating these basic statistics for my data analysis project!

 

Tags (1)
0 Kudos
2 Replies
Sven_Harpering
Esri Contributor

Hi,

you could use the statistics module.

 

import statistics

data = [12, 45, 67, 23, 41, 89, 34, 54, 21]

median = statistics.median(data)
mean = statistics.mean(data)

print(median)
print(mean)

 

Result:

41
42.888888888888886

 

Passionate about GIS and on the journey as an instructor for analytical insights.
DanPatterson
MVP Esteemed Contributor

Or the numpy module

import numpy as np

data = [12, 45, 67, 23, 41, 89, 34, 54, 21]
np.mean(data), np.median(data)
(42.888888888888886, 41.0)

or using the percentile
np.percentile(data, 50)
41.0

... sort of retired...