过滤数据框检查列中的值是否是某个对象

发布于 2025-01-11 12:50:09 字数 334 浏览 1 评论 0原文

我有一个问题。我有一个来自 geopandas 的 GeoDataFrame 对象,其中有一列名为“geometry”的列以及来自 shapely.geometry 的多边形和多重多边形。我想过滤数据框,只留下多边形(知道多边形是对象)。

我尝试过:

gdf ​​= gdf[gdf["geometry"] == shapely.geometry.multipolygon.MultiPolygon]

我想如果几何列中的每个值都是此 shapely 的实例,则可能是“验证”的内容。 Geometry.multipolygon.MultiPolygon 对象。

我怎样才能过滤掉这个地理/数据框?

I have a question. I have a GeoDataFrame object from geopandas with a column named "geometry" and polygons and multipolygons from shapely.geometry. I want to filter the dataframe and only leaving out the multipolygons (knowing that multipolygons are object).

I tried:

gdf = gdf[gdf["geometry"] == shapely.geometry.multipolygon.MultiPolygon]

I guess it could be something along the lines of "validate" if every value in the geometry column, is instance of this shapely.geometry.multipolygon.MultiPolygon object.

How could I filter out this Geo/DataFrame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2025-01-18 12:50:09

loc[]isinstance() 的组合将过滤为仅多边形。下面是完整的工作示例。

import geopandas as gpd
import shapely

world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

world.loc[world["geometry"].apply(lambda g: isinstance(g, shapely.geometry.Polygon))]

a combination of loc[] and isinstance() will filter to just polygons. Full working example below.

import geopandas as gpd
import shapely

world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

world.loc[world["geometry"].apply(lambda g: isinstance(g, shapely.geometry.Polygon))]
雨落□心尘 2025-01-18 12:50:09
gdf = gdf.loc[gdf["geometry"].apply(lambda x: type(x) == shapely.geometry.multipolygon.MultiPolygon]
gdf = gdf.loc[gdf["geometry"].apply(lambda x: type(x) == shapely.geometry.multipolygon.MultiPolygon]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文