Python 在geopanda数据帧中扩展多多边形

Python 在geopanda数据帧中扩展多多边形,python,pandas,shapefile,geopandas,Python,Pandas,Shapefile,Geopandas,我有一个包含多边形和多多边形的形状文件,如下所示: name geometry 0 AB10 POLYGON ((-2.116454759005259 57.14656265903432... 1 AB11 (POLYGON ((-2.052573095588467 57.1342600856536... 2 AB12 (POLYGON ((-2.128066321470298 57.03683

我有一个包含多边形和多多边形的形状文件,如下所示:

   name                                           geometry
0  AB10  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11  (POLYGON ((-2.052573095588467 57.1342600856536...
2  AB12  (POLYGON ((-2.128066321470298 57.0368357386797...
3  AB13  POLYGON ((-2.261525922489881 57.10693578217748...
4  AB14  POLYGON ((-2.261525922489879 57.10693578217748...
第二行和第三行对应于多多边形,而其余的是多边形。 我想将几何体为Multipolygon类型的行展开为多边形行,如下所示

   name                                           geometry
0  AB10  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11  POLYGON ((-2.052573095588467 57.1342600856536...
2  AB11  POLYGON ((-2.045849648028651 57.13076387483844...
3  AB12  POLYGON ((-2.128066321470298 57.0368357386797...
4  AB12  POLYGON ((-2.096125852304303 57.14808092585477
3  AB13  POLYGON ((-2.261525922489881 57.10693578217748...
4  AB14  POLYGON ((-2.261525922489879 57.10693578217748...
请注意,AB11和AB12多边形已扩展到多行,其中每行对应一个多边形数据

我认为这是geopanda数据处理。有没有一种类似蟒蛇的方法来实现上述目标


谢谢大家!

我目前对上述问题的解决方案有两种

第一步。检查每一行,如果类型为multipolygon,则应用列表理解

   name                                           geometry
0  AB10  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11  [POLYGON ((-2.052573095588467 57.1342600856536...
2  AB12  [POLYGON ((-2.128066321470298 57.0368357386797...
3  AB13  POLYGON ((-2.261525922489881 57.10693578217748...
4  AB14  POLYGON ((-2.261525922489879 57.10693578217748...
步骤2:使用将一行中的元素列表扩展为多行的技巧

df.set_index(['name'])['geometry'].apply(pd.Series).stack().reset_index()

  name  level_1                                                  0
0  AB10        0  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11        0  POLYGON ((-2.052573095588467 57.13426008565365...
2  AB11        1  POLYGON ((-2.045849648028651 57.13076387483844...
3  AB12        0  POLYGON ((-2.128066321470298 57.0368357386797,...
4  AB12        1  POLYGON ((-2.096125852304303 57.14808092585477...
5  AB13        0  POLYGON ((-2.261525922489881 57.10693578217748...
6  AB14        0  POLYGON ((-2.261525922489879 57.10693578217748...

请让我知道是否有一种方法可以一步完成

我目前对上述问题的解决方案有两种

第一步。检查每一行,如果类型为multipolygon,则应用列表理解

   name                                           geometry
0  AB10  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11  [POLYGON ((-2.052573095588467 57.1342600856536...
2  AB12  [POLYGON ((-2.128066321470298 57.0368357386797...
3  AB13  POLYGON ((-2.261525922489881 57.10693578217748...
4  AB14  POLYGON ((-2.261525922489879 57.10693578217748...
步骤2:使用将一行中的元素列表扩展为多行的技巧

df.set_index(['name'])['geometry'].apply(pd.Series).stack().reset_index()

  name  level_1                                                  0
0  AB10        0  POLYGON ((-2.116454759005259 57.14656265903432...
1  AB11        0  POLYGON ((-2.052573095588467 57.13426008565365...
2  AB11        1  POLYGON ((-2.045849648028651 57.13076387483844...
3  AB12        0  POLYGON ((-2.128066321470298 57.0368357386797,...
4  AB12        1  POLYGON ((-2.096125852304303 57.14808092585477...
5  AB13        0  POLYGON ((-2.261525922489881 57.10693578217748...
6  AB14        0  POLYGON ((-2.261525922489879 57.10693578217748...

请让我知道是否有一种方法可以一步完成

如果只有两列,我们可以使用numpy来提高速度

如果您有一个数据帧,如

name geometry 0 0 polygn(x) 1 2 (polygn(x), polygn(x)) 2 3 polygn(x) 3 4 (polygn(x), polygn(x)) 输出:

name geometry 0 0 polygn(x) 1 2 polygn(x) 2 2 polygn(x) 3 3 polygn(x) 4 4 polygn(x) 5 4 polygn(x)
如果只有两列,我们可以使用numpy来提高速度

如果您有一个数据帧,如

name geometry 0 0 polygn(x) 1 2 (polygn(x), polygn(x)) 2 3 polygn(x) 3 4 (polygn(x), polygn(x)) 输出:

name geometry 0 0 polygn(x) 1 2 polygn(x) 2 2 polygn(x) 3 3 polygn(x) 4 4 polygn(x) 5 4 polygn(x)
如果没有一些上下文或代码来重现实际的数据帧,这将不会很有帮助。如果没有一些上下文或代码来重现实际的数据帧,这将不会很有帮助。