python code how to find median from describe code example
Example 1: calculate mode in python
from collections import Counter
def calculate_mode(n):
c = Counter(n)
num_freq = c.most_common()
max_count = num_freq[0][1]
modes = []
for num in num_freq:
if num[1] == max_count:
modes.append(num[0])
return modes
def calculate_mode(n):
c = Counter(n)
mode = c.most_common(1)
return mode[0][0]
Example 2: finding median on python
def calculate_median(n):
N = len(n)
n.sort()
if N % 2 == 0:
m1 = N / 2
m2 = (N / 2) + 1
m1 = int(m1) - 1
m2 = int(m2) - 1
median = (n[m1] + n[m2]) / 2
else:
m = (N + 1) / 2
m = int(m) - 1
median = n[m]
return median