Skip to content
Advertisement

Find median of interval data in python

I am exploring different python libraries and I wondering how to find approximate median value of the grouped data set. Here is a table for a reference.

Age frequency
1-10 123
10-20 350
20-30 200
30-40 1700
40-50 360
50-60 60

Is there any function or do I need to hard code the formula and then have a loop going over each row and column of the dataset ?

Thanks in advance.

Advertisement

Answer

If you want to approximate median for discrete grouped data, you can approximate the median of the entire data set by interpolation using the formula:

median = L + interval * (N / 2 – CF) / F

L = lower limit of the median interval

N = total number of data points

CF = number of data points below the median interval

F = number of data points in the median interval

JavaScript

L1 = 31 , cumsum_before = 673, freq_medain = 1700, width = 10
Approximated median = 35.25588235294118

If you have continuous data, you can use median_grouped function in statistics package.

JavaScript

Approximated median = 35.25588235294118

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement