简体   繁体   English

如何在特定单元格位置查找单元格数据值列表?

[英]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.在给定的条件下,我有下一个代码到 select 单元格 position 。 This is from a raster.这是来自栅格。 As you'll see I'm using NumPy and Pandas for this:如您所见,我为此使用 NumPy 和 Pandas :

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

When I print f I get something like this:当我打印 f 我得到这样的东西:

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.现在有了这个先前的结果,我想获得的是该单元格 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.我想到了类似下一个代码的东西,但我只得到一个值(最后一个对应于 f 中的最后一个单元格),而不是一个数组。 The result B is a NumPy array but a single number is showing:结果 B 是一个 NumPy 数组,但显示了一个数字:

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.如您所见,它不是从每行的栅格中返回单元格数据值的列表或数组,单元格 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.我有超过 200 列和行,所以使用类似于我上面显示的内容更容易,但我还没有找到方法。 Any suggestion is very appreciated, thanks!任何建议都非常感谢,谢谢!

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:您还可以将您的列表转换为 numpy 数组并为所有这些调用一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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