如何对数据框中位于 geopandas 边界内的条目数求和

发布于 2025-01-11 23:21:09 字数 345 浏览 0 评论 0原文

我有两个地理数据框:一个包含名为 bird_df 的特定物种动物的目击数据(以位置为点),另一个详细说明了我所在州内每个城市的边界,名为 map_df >。

我想使用 geopandas 方法 .contains(x) 来计算每个城市中发现的动物数量,并将总数添加到边界数据框中,以便我可以生成分区统计图。

我的熊猫有点生锈了,但我尝试过类似的事情 map_df[map_df["geometry"].contains(bird_df["geometry"]) == True]

我只是不知道如何解决这个问题。一些帮助将不胜感激。

谢谢

I have two geodataframes: One containing sightings of animals of a specific species called bird_df (with location as points), and the other detailing the boundaries of each municipality within my state called map_df.

I want to use the geopandas method .contains(x) to count the number of animals that were found in each municipality and add that total to the boundaries dataframe so i can generate a choropleth.

My pandas is a bit rusty but I've tried things like
map_df[map_df["geometry"].contains(bird_df["geometry"]) == True]

I just don't know how to wrap my head around this problem. Some help would be appreciated.

Thanks

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

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

发布评论

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

评论(1

喜你已久 2025-01-18 23:21:09

我建议为此使用 sjoin。

import geopandas as gpd
import pandas as pd

# make some dummy points
df = pd.DataFrame()
df['x'] = [0.61, 0.62, 0.63, 0.64, 0.65]
df['y'] = [41.60, 41.62, 41.63, 41.64, 41.65]
points_gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(df.x, df.y))

# buffer 2 rows of points to make some dummy polys
poly_gdf = points_gdf.head(2).copy(deep=True)
poly_gdf['geometry'] = poly_gdf['geometry'].buffer(0.02)

# do the spatial join, index right is the polygon idx values
sjoin_gdf = gpd.sjoin(points_gdf, poly_gdf)

# count the values with value counts
count_dict = sjoin_gdf['index_right'].value_counts().to_dict()

# map the count_dict back to poly_gdf as new point count column 
# alternatively you could do a join here, but new col name is nice
poly_gdf['point_count'] = poly_gdf.index.map(count_dict)

输出poly_gdf:

    geometry                                            point_count
0   POLYGON ((0.63000 41.60000, 0.62990 41.59804, ...   1
1   POLYGON ((0.64000 41.62000, 0.63990 41.61804, ...   2

即:

cat_df = pd.concat([points_gdf, poly_gdf])
cat_df.plot(facecolor="none")

在此处输入图像描述

I recommend using an sjoin for this.

import geopandas as gpd
import pandas as pd

# make some dummy points
df = pd.DataFrame()
df['x'] = [0.61, 0.62, 0.63, 0.64, 0.65]
df['y'] = [41.60, 41.62, 41.63, 41.64, 41.65]
points_gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(df.x, df.y))

# buffer 2 rows of points to make some dummy polys
poly_gdf = points_gdf.head(2).copy(deep=True)
poly_gdf['geometry'] = poly_gdf['geometry'].buffer(0.02)

# do the spatial join, index right is the polygon idx values
sjoin_gdf = gpd.sjoin(points_gdf, poly_gdf)

# count the values with value counts
count_dict = sjoin_gdf['index_right'].value_counts().to_dict()

# map the count_dict back to poly_gdf as new point count column 
# alternatively you could do a join here, but new col name is nice
poly_gdf['point_count'] = poly_gdf.index.map(count_dict)

Output poly_gdf:

    geometry                                            point_count
0   POLYGON ((0.63000 41.60000, 0.62990 41.59804, ...   1
1   POLYGON ((0.64000 41.62000, 0.63990 41.61804, ...   2

Viz:

cat_df = pd.concat([points_gdf, poly_gdf])
cat_df.plot(facecolor="none")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文