P-robot P-robot - 7 months ago 89
Python Question

How to subset geopandas dataframe based upon coordinates of points?

I would like to subset a geopandas dataframe based upon values of the first coordinate of a column of points (i.e. the x values).

import geopandas as gpd
from shapely.geometry import Point

gdf = gpd.GeoDataFrame([Point((0,0)), Point((4,2)),Point((2,4))])
gdf.columns = ['points']

So, given the above data, I would like to check if the first coordinate is greater than 3 (for instance) so this would return an array of
[False, True, False]
so that I can then subset the original dataframe.


To check if the x coordinate is greater than 3 you can use apply to extract the coordinate from the points:

gdf[gdf.points.apply(lambda p: p.x) > 3]