Find points in cells through pandas dataframes of coordinates

邮差的信 提交于 2019-12-01 01:53:22

Probably a better way, but since this has been sitting out there for awhile..

Using Pandas boolean indexing to filter the dfc data frame instead of np.where()

def findGrid(dfp):  
    c = dfc[(dfp['x'] > dfc['minx']) &
            (dfp['x'] < dfc['maxx']) &
            (dfp['y'] > dfc['miny']) &
            (dfp['y'] < dfc['maxy'])].Code

    if len(c) == 0:        
        return None
    else:        
        return c.iat[0]

Then use the pandas apply() function

dfp['GridCode'] = dfp.apply(findGrid,axis=1)

Will yield this

    Id  x   y   GridCode
0   0   1.5 1.5 1
1   1   1.1 1.1 1
2   2   2.2 2.2 2
3   3   1.3 1.3 1
4   4   3.4 1.4 NaN
5   5   2.0 1.5 NaN
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!