Search code examples
pythonexcelnumpymedian

Find a median in Python from Excel file


I am looking to find the median as floats in the Excel which is longitude and contains (-68.2288284302 ~ -159.4401397705)

However, based on my code it returns 173.3871917725 which is not make sense.. right ? It suppose to return around -91.xxxxx based on calculation in Excel.

below is my code:

for i in range(2, maxRow+1):        
    country = sheet.cell(i,7).value
    longitude = sheet.cell(i,6).value
    median = np.median(longitude)
    if country=="United States":
        print("United States " + str(median))
print(median)

below is the ss for output.

output

So what i concern is how can i find the right number of median ?

Could anyone help me with this issue ?


Solution

  • You are computing median on a single value, you should build a list and then compute

    the median:
    
    longitude_list = []
    for i in range(2, maxRow+1):        
        country = sheet.cell(i,7).value
        longitude = sheet.cell(i,6).value
        longitude_list.append(longitude)
    median = np.median(longitude)
    print(median)