5 examples of 'how to find median of grouped data' in Python

Every line of 'how to find median of grouped data' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
26def median(values):
27 length = len(values)
28 values.sort()
29 if length % 2 != 0:
30 # Odd number of values, so chose middle one
31 return values[length/2]
32 else:
33 # Even number of values, so mean of middle two
34 return mean([values[length/2], values[(length/2)-1]])
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

106def median(x):
107 return sorted(x)[len(x) // 2]
136def findMedian(self):
137 small, large = self.heaps
138 if len(large) > len(small):
139 return float(large[0])
140 return (large[0] - small[0]) / 2.0
564def _reduce(self, data):
565 if not HAS_NUMPY:
566 return sorted(data)[len(data) // 2]
567
568 return np.median(data)
42def median(numbers):
43 """Return the median of the list of numbers.
44
45 found at: http://mail.python.org/pipermail/python-list/2004-December/253517.html"""
46 # Sort the list and take the middle element.
47 n = len(numbers)
48 copy = numbers[:] # So that "numbers" keeps its original order
49 copy.sort()
50 if n & 1: # There is an odd number of elements
51 return copy[n // 2]
52 else:
53 return (copy[n // 2 - 1] + copy[n // 2]) / 2.0

Related snippets