I have a question on Geopandas.
I have a shape file for a country which I am loading into geopandas. The shape file has shape for all states in country.
I need to extract a single state. I have tried exploring documentation and stackoverflow but was unable to warp my mind around how to use query()
method to retrieve the single state.
import geopandas as gpd
import os
# get current directory path
cur_dir = os.path.dirname(os.path.realpath(__file__))
gdf_shp_state = gpd.read_file(cur_dir + '/data/StateBoundary/StateBoundary.shp')
print(type(gdf_shp_state))
print(gdf_shp_state)
Output:
<class 'geopandas.geodataframe.GeoDataFrame'>
state geometry
0 ANDAMAN & NICOBAR (POLYGON ((10341718.474 1449533.160500001, 103...
1 CHANDIGARH POLYGON ((8546255.616099998 3606050.813100003,...
2 DADAR & NAGAR HAVELI (POLYGON ((8137193.4859 2315664.964499999, 813...
3 DAMAN & DIU (POLYGON ((8111624.4714 2328002.898499999, 811...
4 DELHI POLYGON ((8583390.569699999 3359116.190099999,...
5 HARYANA POLYGON ((8524318.5392 3516490.864500001, 8524...
6 JHARKHAND POLYGON ((9762288.284699999 2772949.712499999,...
Question is : How do I query the GeoDataFrame?
There is a method called gdf_shp_state.query()
and its expecting me to put a query as a string
. Can someone help me provide an example query string which I need to use to get GeoDataFrame for single state?
geodataframes work the same way as regular dataframes, so the query here could be
gdf_shp_state.query("state=='ANDAMAN & NICOBAR'")
To select the row(s) where state equals 'ANDAMAN & NICOBAR'
The above is equivalent to:
gdf_shp_state[gdf_shp_state.state == 'ANDAMAN & NICOBAR']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.