Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Statistics - Median


The median is a type of average value, which describes where the center of the data is located.


Median

The median is the middle value in a data set ordered from low to high.


Finding the Median

The median can only be calculated for numerical variables.

The formula for finding the middle value is:

\( \displaystyle \frac{n + 1}{2} \)

Where \(n\) is the total number of observations.

If the total number of observations is an odd number, the formula gives a whole number and the value of this observation is the median.

13, 21, 21, 40, 48, 55, 72

Here, there are 7 total observations, so the median is the 4th value:

\( \displaystyle \frac{7 + 1}{2} = \frac{8}{2} = 4 \)

The 4th value in the ordered list is 40, so that is the median.

If the total number of observations is an even number, the formula gives a decimal number between two observations.

13, 21, 21, 40, 42, 48, 55, 72

Here, there are 8 total observations, so the median is between the 4th and 5th values:

\( \displaystyle \frac{8 + 1}{2} = \frac{9}{2} = 4.5 \)

The 4th and 5th values in the ordered list is 40 and 42, so the median is the mean of these two values. That is, the sum of those two values divided by 2:

\( \displaystyle \frac{40+42}{2} = \frac{82}{2} = \underline{41} \)

Note: It is important that the numbers are ordered before you can find the median.



Finding the Median with Programming

The median can easily be found with many programming languages.

Using software and programming to calculate statistics is more common for bigger sets of data, as finding it manually becomes difficult.

Example

With Python use the NumPy library median() method to find the median of the values 13, 21, 21, 40, 42, 48, 55, 72:

import numpy

values = [13,21,21,40,42,48,55,72]

x = numpy.median(values)

print(x)
Try it Yourself »

Example

Use the R median() function to find the median of the values 13, 21, 21, 40, 42, 48, 55, 72:

values <- c(13,21,21,40,42,48,55,72)

median(values)
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.