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