The Weighted Mean and the Median : 5. Distributions with Even Number of Values

Screen Link:
https://app.dataquest.io/m/306/the-weighted-mean-and-the-median/5/distributions-with-even-number-of-values

My Code:

houses_median = houses['TotRms AbvGrd'].copy()

houses_median = houses_median.replace('10 or more' , 10)

houses_median = houses_median.astype(int)
houses_median.sort_values(ascending=True, inplace=True)

# Find the median
length =  len(houses_median)

if length // 2 != 0:
    median = houses_median[length//2]
else:
    median = (houses_median[length//2] + length[(length//2)-1])/2

What I expected to happen:
Our task is to first check the length of the series and based on whether its odd or even calculate the median. My code does exactly that.

When I analysed the code provided by DQ, I understand the code works fine when the length of the series is even(which is the case with our example). But how would the code work correctly when the length of the series is odd? The variable ‘middle_indices’ is going to provide two indices no matter the whether the length of the series is odd or even. But that should not be the case.

Please provide your thoughts.

The code provided by DQ:

# Find the median
middle_indices = [int((len(rooms_sorted) / 2) - 1),
                  int((len(rooms_sorted) / 2))
                 ] # len - 1 and len because Series use 0-indexing 
middle_values = rooms_sorted.iloc[middle_indices] # make sure you don't use loc[]
median = middle_values.mean()
1 Like

Great piece of code you’ve got there!

With that said, the title of this mission explicitly deals with distributions with an even number of values, so the solution code being used is appropriate given the context.

Well you are right. The title of this mission explicitly deals with distributions with an even number of values. But what created the confusion was the instructions for executing this mission. The last instruction (as shown below) made me think that I should create one scenario to handle both the cases.

Thanks for your response.

2 Likes