将具有类似 geojson 列的 pandas 数据框转换为 Geopandas 数据框

问题描述 投票:0回答:1

我有一个 pandas 数据框

df_uk
,其中有一列
Geo shape
,看起来像 geojson 数据:

Geo shape
元素如下:

{“坐标”:[[[-5.358386758461411, 36.141109447000375], [-5.338773483242616, 36.14111967199992], [-5.339914516999954, 36.1 2982819200005], [-5.339060024999924, 36.12384674700007], [-5.34203040299991, 36.11050039300005], [-5.35024980399993, 36.119289 45500006] , [-5.358386758461411, 36.141109447000375]]], "类型": "多边形"}

“类型”可以是“多边形”或“多多边形”。

我想将此 pandas 数据框转换为 Geopandas 数据框,并使用

Geo Shape
列作为几何列。

我怎样才能做到这一点?

非常感谢您的帮助。

pandas dataframe geojson geopandas
1个回答
0
投票

您可以将

shape
应用于每个元素,并将其传递给
geometry
GeoDataFrame
:

from shapely.geometry import shape

gdf = gpd.GeoDataFrame(df, geometry=df.pop("Geo Shape").apply(shape))

输出:

print(gdf)

       SOVEREIGNT      NAME                                           geometry
0  United Kingdom  Scotland  POLYGON ((-5.35839 36.14111, -5.33877 36.14112...

# <class 'geopandas.geodataframe.GeoDataFrame'>

使用的输入:

df = pd.DataFrame(
    {'SOVEREIGNT': ['United Kingdom'], 'NAME': ['Scotland'],
     'Geo Shape': [{'coordinates':
                    [[[-5.358386758461411, 36.141109447000375],
                      [-5.338773483242616, 36.14111967199992],
                      [-5.339914516999954, 36.12982819200005],
                      [-5.339060024999924, 36.12384674700007],
                      [-5.34203040299991, 36.11050039300005],
                      [-5.35024980399993, 36.11928945500006],
                      [-5.358386758461411, 36.141109447000375]]],
                    'type': 'Polygon'}]})
© www.soinside.com 2019 - 2024. All rights reserved.