【问题标题】:Finding cell location in python在python中查找单元格位置
【发布时间】:2019-01-06 11:09:06
【问题描述】:

我对 python 比较陌生,并试图找到包含值“3275”的单元格,即“newELA”。该值位于电子表格的第一行,是一个标题。这是我一直在尝试的:

loc=("/Volumes/Project/Andes_Glacier_Inventory.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(1)
headers = sheet.row(0)[3:]

a = np.array(sheet.row_values(1,3))

value = 501
ELA = headers[np.argmax(a * (a < value))]
print ("The ELA is", ELA.value)

changeinELA = 100
value1 = changeinELA
value2 = ELA.value
newELA = float(value1) + float(value2)
print ("The new ELA is", newELA)

b = np.where (np.array(headers) == newELA)
print (b)

我得到的结果是,我什至不明白

(array([], dtype=int64),)

【问题讨论】:

  • 能否提供完整的代码,比如sheet是如何获得的?
  • 您的结果似乎是一个空的 numpy 数组。是的,正如@Pierre 建议的那样,你能提供更多代码吗?
  • 用我目前使用的所有代码编辑
  • b== 为真的索引 - 在这种情况下没有任何索引。如果您向我们展示了headersnewELA,它可能会有所帮助。

标签: python arrays numpy header


【解决方案1】:

你可以看到How does python numpy where work?。值“3275”是一个字符串。在 另一方面,您有一个整数数组,而 newELA 是浮点数。您必须决定 headers 数组是哪个 dtype,它应该与 newELA 变量相同。例如,

import numpy as np
headers = [200, 100, 300]
a = np.array(headers)
b = np.where (a == 300)
print(b)

输出

(array([2], dtype=int64),)

【讨论】:

    【解决方案2】:

    您得到一个空数组,因为您的 headers 中没有任何内容等于您的 newELA 值。

    检查您的数据。想一想您的问题可能是什么:如果出现浮点错误,您可以执行以下操作:

    tol = 1e-10   #change accordingly
    b = np.where (np.abs(np.array(headers, dtype=np.float) - newELA) < tol) #passing dtype=np.float will convert your strings, if you need to
    print (b) 
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多