首页 > 解决方案 > 使用 Geopandas 将 NYC shapefile 子集到曼哈顿

问题描述

我有一个纽约市的 shapefile,我想使用 geopandas 将其缩减为仅覆盖曼哈顿。(来源:https ://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page )。

感谢您的任何建议!!!

已经尝试过https://mygeodata.cloud/但无法让它工作......

我的总体目标是稍后检查某些纬度/经度点是否在曼哈顿的 shapefile 中,但目前,它会为纽约市执行此操作,因此包括实际上在曼哈顿之外的点。

标签: pythonshapefilegeopandas

解决方案


假设您正在使用以下 shapefile: 在此处输入图像描述

除了你的几何列之外GeoDataFrame,它的作用就像熊猫一样DataFrame。因此,您可以GeoDataFrame像使用 Pandas 一样对您的喜好进行子集化。

import geopandas as gpd

gdf = gpd.read_file('taxi_zones.shp')

# only keep Manhattan
gdf = gdf[gdf.borough == 'Manhattan']

# you can check it:
gdf.borough.value_counts()

推荐阅读