在python中查找单元格位置
我对python比较陌生,我试图找到包含值“3275”的单元格,这里是“newELA”。该值位于电子表格的顶行,是一个标题。这就是我一直在尝试的:在python中查找单元格位置,python,arrays,numpy,header,Python,Arrays,Numpy,Header,我对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))
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)
你可以看到。值“3275”是一个字符串。上
另一方面,您有一个整数数组,newELA是float。您必须决定headers数组的数据类型,并且它应该与newELA变量相同。比如说,
import numpy as np
headers = [200, 100, 300]
a = np.array(headers)
b = np.where (a == 300)
print(b)
输出
(array([2], dtype=int64),)
您得到的是一个空数组,因为
标题中没有与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)
tol=1e-10#相应更改
b=np.where(np.abs(np.array(headers,dtype=np.float)-newELA)
您能否提供完整的代码,例如如何获得工作表
?您的结果似乎是一个empy numpy数组。是的,正如@Pierre所建议的,你能提供更多的代码吗?用我目前使用的所有代码编辑的b
是=
为真的索引-在这种情况下没有任何索引。如果您向我们展示标题
和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)