Supernova
Supernova

Reputation: 99

How to find list of cell data value at a specific cell positions?

I have this next code to select the cell position at a given condition. This is from a raster. As you'll see I'm using NumPy and Pandas for this:

f = np.column_stack(np.where(demData>0))

When I print f I get something like this:

print(f) 

[[  0  89]
 [  0  90]
 [  0  91]
 [  1  90]
 [  1  91]
 [  1  92]
 [  1  93]
 [  2  92]
 [  2  93]]

Now with this previous result, what I'd like to obtain is the data value at that cell position. For example:

df = pd.DataFrame(demDataStage)
df.at[1,93]

And, the result:

618.965

I have in mind something like this next code but I only get a single value (the last one corresponding to the last cell in f) but not an array. The result B is a NumPy array but a single number is showing:

row = f[:,0]
col = f[:,1] 

for i in row:
    for c in col:
        B = df.at[i,c]
print(B)
720.123

As you can see it's not a list or array that returns the cell's data value from the raster at each row,col cell position. I have more that 200 columns and rows so it's easier with something similar to what I showed above but I haven't found the way yet. Any suggestion is very appreciated, thanks!

Upvotes: 3

Views: 852

Answers (1)

Ehsan
Ehsan

Reputation: 12417

You can simply get your list by appending to it:

B = []
for i in row:
    for c in col:
        B.append(df.at[i,c])

You can also convert your list to numpy array and call a single line for all this:

B = df.to_numpy()[demData>0]

Upvotes: 1

Related Questions