geopandas.GeoDataFrame.sjoin#
- GeoDataFrame.sjoin(df, *args, **kwargs)[source]#
Spatial join of two GeoDataFrames.
See the User Guide page Merging data for details.
- Parameters
- dfGeoDataFrame
- howstring, default ‘inner’
The type of join:
‘left’: use keys from left_df; retain only left_df geometry column
‘right’: use keys from right_df; retain only right_df geometry column
‘inner’: use intersection of keys from both dfs; retain only left_df geometry column
- predicatestring, default ‘intersects’
Binary predicate. Valid values are determined by the spatial index used. You can check the valid values in left_df or right_df as
left_df.sindex.valid_query_predicates
orright_df.sindex.valid_query_predicates
- lsuffixstring, default ‘left’
Suffix to apply to overlapping column names (left GeoDataFrame).
- rsuffixstring, default ‘right’
Suffix to apply to overlapping column names (right GeoDataFrame).
See also
GeoDataFrame.sjoin_nearest
nearest neighbor join
sjoin
equivalent top-level function
Notes
Every operation in GeoPandas is planar, i.e. the potential third dimension is not taken into account.
Examples
>>> import geodatasets >>> chicago = geopandas.read_file( ... geodatasets.get_path("geoda.chicago_commpop") ... ) >>> groceries = geopandas.read_file( ... geodatasets.get_path("geoda.groceries") ... ).to_crs(chicago.crs)
>>> chicago.head() community ... geometry 0 DOUGLAS ... MULTIPOLYGON (((-87.60914 41.84469, -87.60915 ... 1 OAKLAND ... MULTIPOLYGON (((-87.59215 41.81693, -87.59231 ... 2 FULLER PARK ... MULTIPOLYGON (((-87.62880 41.80189, -87.62879 ... 3 GRAND BOULEVARD ... MULTIPOLYGON (((-87.60671 41.81681, -87.60670 ... 4 KENWOOD ... MULTIPOLYGON (((-87.59215 41.81693, -87.59215 ...
[5 rows x 9 columns]
>>> groceries.head() OBJECTID Ycoord ... Category geometry 0 16 41.973266 ... NaN MULTIPOINT (-87.65661 41.97321) 1 18 41.696367 ... NaN MULTIPOINT (-87.68136 41.69713) 2 22 41.868634 ... NaN MULTIPOINT (-87.63918 41.86847) 3 23 41.877590 ... new MULTIPOINT (-87.65495 41.87783) 4 27 41.737696 ... NaN MULTIPOINT (-87.62715 41.73623) [5 rows x 8 columns]
>>> groceries_w_communities = groceries.sjoin(chicago) >>> groceries_w_communities[["OBJECTID", "community", "geometry"]].head() OBJECTID community geometry 0 16 UPTOWN MULTIPOINT (-87.65661 41.97321) 87 365 UPTOWN MULTIPOINT (-87.65465 41.96138) 90 373 UPTOWN MULTIPOINT (-87.65598 41.96297) 140 582 UPTOWN MULTIPOINT (-87.67417 41.96977) 1 18 MORGAN PARK MULTIPOINT (-87.68136 41.69713)