【问题标题】:Find a median in Python from Excel file从 Excel 文件中查找 Python 中的中位数
【发布时间】:2021-04-22 11:45:13
【问题描述】:

我希望在 Excel 中找到作为浮点数的中位数,它是经度并包含 (-68.2288284302 ~ -159.4401397705)

但是,根据我的代码,它返回 173.3871917725 这是没有意义的.. 对吗?根据 Excel 中的计算,它应该返回 -91.xxxxx 左右。

下面是我的代码:

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)

下面是输出的ss。

output

所以我关心的是如何找到正确的中位数?

谁能帮我解决这个问题?

【问题讨论】:

  • 我的第一个建议是从图片中提取 Excel:将经度值保存在列表中。其次,这似乎不是 NumPy 的中位数的工作方式:中位数需要一个列表本身,您可以尝试将之前保存的值列表传递给它:a=[] for i in range(2, maxRow+1): a.append(sheet.cell(i,6).value) a=[-73, -157, -87, -118, -122] # suppose it's from Excel >>> np.median(a) # -118.0

标签: python excel numpy median


【解决方案1】:

你正在计算单个值的中位数,你应该建立一个列表然后计算

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)

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多