Why am I getting a median of 7.5 and not 6?

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

My Code:

abovegnd = houses['TotRms AbvGrd'].copy()
abovegnd = abovegnd.replace('10 or more',10)
abovegnd = abovegnd.astype(int)
abovegnd = abovegnd.sort_values(ascending=True)

print('Lenght of array is: ',len(abovegnd))
print('The median should be: ',(abovegnd[1465]+abovegnd[1464])/2)

if (len(abovegnd) % 2) == 0:
    print('Even number of values')
    idx1 = int(len(abovegnd)/2)
    print('First middle index is: ',idx1)
    idx2 = idx1 - 1
    print('Second middle index is: ',idx2)
    median = (abovegnd[idx1] + abovegnd[idx2]) / 2
else:
    print('Odd number of values')
    idx = len(abovegnd)//2
    print('index of middle value is: ',idx)
    median = abovegnd[idx]

What I expected to happen:

I expected to get the same result as the solution: 6

What actually happened:

The following is printed out:

Lenght of array is:  2930
The median should be:  7.5
Even number of values
First middle index is:  1465
Second middle index is:  1464

Note that I get the SAME indices as the solution says. Why is the result different? I get a median of 7.5

Could you post the screen link please?

This can be a tricky one to figure out initially.

This is your code

abovegnd = abovegnd.sort_values(ascending=True)

From above some of your initial values would be something like -

1901 2
2679 3
975 3
269 3
1600 3

The above is a Pandas Series. For the above, how would you get the value that I have highlighted (in bold)?

Would it be abovegnd[3]? Because it’s at the 3rd index?

Or would it be abovegnd[269]?

When you do abovegnd[1464], you are going to get the value at the index 1464. And because you sorted your values in abovegnd, that 1464 is not the middle index anymore.

That’s why, in the solution, they have used iloc instead. Because iloc returns the value based on the position itself and not in the actual index value. So, abovegnd.iloc[3] above would return that highlighted value.