简体   繁体   中英

Finding cell location in python

I am relatively new to python and trying to locate the cell that contains the value "3275" which is here, the "newELA". This value is in the top row of the spreadsheet and is a header. This is what I have been trying:

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)

The results I am getting are, which I don't even understand

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

You can see How does python numpy where work? . The value "3275" is a string. On the other hand, you have an array of integers and the newELA is float. You have to decide in which dtype the headers array is and it should be the same with the newELA variable. For example,

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

output

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

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM