Filter a GeoPandas dataframe for points within a specific country
Did you see More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc and other answers on GIS SE ?
Simply
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
uk = world[world.name == "United Kingdom"]
type(uk)
geopandas.geodataframe.GeoDataFrame
So uk is a GeoDataFrame
uk.head()
pop_est continent name iso_a3 gdp_md_est geometry
57 62262000.0 Europe United Kingdom GBR 1977704.0 (POLYGON ((-5.661948614921897 54.5546031764838...
The points shapefile:
points = gpd.read_file('uk_points.shp')
points.head()
FID geometry
0 0.0 POINT (-0.0893 51.4735)
1 1.0 POINT (-0.0894 51.4732)
2 2.0 POINT (-0.0898 51.4717)
3 3.0 POINT (-0.0907 51.4727)
4 4.0 POINT (-0.0901 51.4723)
And now
from geopandas.tools import sjoin
pointInPolys = sjoin(points, uk, how='left')
pointInPolys.head()
FID geometry index_right pop_est continent name iso_a3 gdp_md_est
0 0.0 POINT (-0.0893 51.4735) 0 62262000.0 Europe United Kingdom GBR 1977704.0
1 1.0 POINT (-0.0894 51.4732) 0 62262000.0 Europe United Kingdom GBR 1977704.0
2 2.0 POINT (-0.0898 51.4717) 0 62262000.0 Europe United Kingdom GBR 1977704.0
3 3.0 POINT (-0.0907 51.4727) 0 62262000.0 Europe United Kingdom GBR 1977704.0
4 4.0 POINT (-0.0901 51.4723) 0 62262000.0 Europe United Kingdom GBR 1977704.0
uk = world.ix[world['name']=='United Kingdom']
uk_mask = momdata.within(uk.loc[0, 'geometry'])
uk_momdata = momdata.loc[uk_mask]
uk_momdata
Taken from the following tutorial: https://automating-gis-processes.github.io/2017/lessons/L3/point-in-polygon.html