在python中查找单元位置

2024-09-29 23:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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)

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

^{pr2}$

Tags: theisvaluenparraylocsheetheaders
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) 

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

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

输出

^{pr2}$

相关问题 更多 >

    热门问题