在python中查找单元格位置

时间:2019-01-06 11:09:07

标签: 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))

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),)

2 个答案:

答案 0 :(得分:0)

您可以看到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),)

答案 1 :(得分:0)

You are getting an empty array because there is nothing in your headers which is equal to your newELA value.

Check your data. Just a thought of what could be your problem: If there is a floating point error, you can do the following:

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)