Python 如何使用geopandas对shapefile中的要素进行sjoin迭代,然后对分类数据进行编码?
我有两个shapefile()-一个点层和一个多边形层。点图层表示客户及其位置,而多边形图层表示两个边界。目标是获得以下格式的表格: 顾客 地点1 地点2 1. 1. 1. 2. 0 1. 3. 1. 1. 5. 1. 0 6. 1. 0 9 0 0 10 0 0Python 如何使用geopandas对shapefile中的要素进行sjoin迭代,然后对分类数据进行编码?,python,pandas,geopandas,Python,Pandas,Geopandas,我有两个shapefile()-一个点层和一个多边形层。点图层表示客户及其位置,而多边形图层表示两个边界。目标是获得以下格式的表格: 顾客 地点1 地点2 1. 1. 1. 2. 0 1. 3. 1. 1. 5. 1. 0 6. 1. 0 9 0 0 10 0 0 您不需要连接,intersects方法就足够了。您的目标结构可以通过以下方式实现: points_in_locations = points.copy() for idx, row in polygons.iterrows():
您不需要连接,
intersects
方法就足够了。您的目标结构可以通过以下方式实现:
points_in_locations = points.copy()
for idx, row in polygons.iterrows():
is_in_polygon = points.intersects(row.geometry)
points_in_locations[f"location {idx + 1}"] = is_in_polygon.astype(int)
导致:
id geometry location 1 location 2
0 1 POINT (103.87728 1.30449) 0 1
1 2 POINT (103.87723 1.30415) 0 1
2 3 POINT (103.87761 1.30408) 0 1
3 1 POINT (103.87680 1.30287) 1 0
4 5 POINT (103.87724 1.30288) 1 0
5 6 POINT (103.87710 1.30275) 1 0
6 3 POINT (103.87687 1.30270) 1 0
7 9 POINT (103.87669 1.30444) 0 0
8 10 POINT (103.87681 1.30396) 0 0
位置[f“location{idx+1}”]中的点[u]中的“f”是做什么的?这只是编写
“location{}.format(idx+1)
,即创建列标题的一个步骤。赋值语句就是df[column\u name]=series