将带有类似 geojson 列的 pandas 数据帧转换为 Geopandas 数据帧

Convert pandas dataframe with a geojson-like column to Geopandas dataframe

提问人:boathon 提问时间:11/15/2023 最后编辑:boathon 更新时间:11/15/2023 访问量:41

问:

我有一个 pandas 数据帧,其中有一列看起来像 geojson 数据:df_ukGeo shape

enter image description here

Geo shape元素如下:

{“坐标”: [[[-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”: “多边形”}

“type” 可以是 “Polygon” 或 “MultiPolygon”。

我想将这个 pandas 数据帧转换为 Geopandas 数据帧,并将列作为几何列。Geo Shape

我该怎么做?

非常感谢您的帮助。

数据样本:

{'Unnamed: 0': [295, 114, 225],
 'SOVEREIGNT': ['United Kingdom', 'United Kingdom', 'United Kingdom'],
 'NAME': ['Jersey', 'Anguilla', 'Isle of Man'],
 'Geo Shape': ['{"coordinates": [[[-2.08267167899993, 49.26023997600004], [-2.067860480999911, 49.25067780200004], [-2.020659959999932, 49.235256252000056], [-2.021962042999917, 49.225490627000056], [-2.017323370999918, 49.22138092700004], [-2.011870897999927, 49.215277411000045], [-2.008290167999917, 49.21246979400007], [-2.016997850999928, 49.20722077000005], [-2.020985480999911, 49.20075104400007], [-2.022043423999946, 49.19245026200008], [-2.021962042999917, 49.18138255400004], [-2.024525519999941, 49.17133209800005], [-2.031158006999931, 49.17145416900007], [-2.040028449999909, 49.17572663000004], [-2.049224412999934, 49.17829010600008], [-2.079986131999931, 49.17869700700004], [-2.096994594999956, 49.183661200000074], [-2.120228644999941, 49.19916413000004], [-2.128895636999914, 49.20307038000004], [-2.137928839999915, 49.20563385600008], [-2.14484615799995, 49.20563385600008], [-2.152943488999938, 49.19993724200003], [-2.160633917999917, 49.190741278000075], [-2.168690558999913, 49.17829010600008], [-2.177154100999928, 49.18016185100004], [-2.18968665299991, 49.189113674000055], [-2.200103318999936, 49.19196198100008], [-2.202951626999948, 49.19041575700004], [-2.223378058999913, 49.18471914300005], [-2.233631964999915, 49.18451569200005], [-2.226470506999931, 49.20600006700005], [-2.233225063999953, 49.228501695000034], [-2.242014126999948, 49.24795156500005], [-2.241118943999936, 49.26023997600004], [-2.229359503999945, 49.263006903000075], [-2.215199347999942, 49.26023997600004], [-2.196278449999909, 49.25340403900009], [-2.182728644999941, 49.25405508000006], [-2.172352667999917, 49.25625234600005], [-2.162709113999938, 49.26040273600006], [-2.151682094999956, 49.26703522300005], [-2.143788214999915, 49.26186758000006], [-2.121978318999936, 49.25828685100004], [-2.110666469999956, 49.25340403900009], [-2.103871222999942, 49.25820547100005], [-2.097564256999931, 49.25967031500005], [-2.08267167899993, 49.26023997600004]]], "type": "Polygon"}',
  '{"coordinates": [[[[-63.00739498599995, 18.27301666900007], [-62.98399817599994, 18.27643463700008], [-62.972645636999914, 18.275864976000037], [-62.97288977799991, 18.269273179000038], [-62.99290930899991, 18.236883856000077], [-63.000559048999946, 18.227362372000073], [-63.01154537699995, 18.220445054000038], [-63.037668423999946, 18.212958075000074], [-63.09951738199993, 18.176174221000053], [-63.10236568899995, 18.18040599200009], [-63.10912024599992, 18.18504466400009], [-63.113840298999946, 18.189846096000053], [-63.136830206999946, 18.17340729400007], [-63.150502081999946, 18.169094143000052], [-63.16783606699994, 18.16933828300006], [-63.14207923099991, 18.198146877000056], [-63.13434811099995, 18.20408763200004], [-63.12279212099992, 18.20807526200008], [-63.09723873599995, 18.21246979400007], [-63.08584550699993, 18.217718817000048], [-63.06777910099993, 18.236476955000057], [-63.05508378799993, 18.254339911000045], [-63.03819739499994, 18.267726955000057], [-63.00739498599995, 18.27301666900007]]], [[[-63.42796790299991, 18.59284088700008], [-63.42882239499994, 18.601263739000046], [-63.42357337099992, 18.600043036000045], [-63.42796790299991, 18.59284088700008]]], [[[-62.95820458534228, 18.28942162673183], [-62.95067631598569, 18.292131803700205], [-62.94766500824305, 18.297251026862686], [-62.93381299262692, 18.294540849894315], [-62.92568246172179, 18.289722757506095], [-62.93200620798133, 18.287313711311985], [-62.94495483127468, 18.283700142020816], [-62.94736387746879, 18.280387703503916], [-62.95669893147096, 18.27436508801864], [-62.96001136998786, 18.27767752653554], [-62.95609666992243, 18.28249561892376], [-62.95820458534228, 18.28942162673183]]], [[[-63.252030887180354, 18.272969970560457], [-63.26147434826127, 18.276246273384444], [-63.262630690434435, 18.28202798425031], [-63.25357267674458, 18.28684607663853], [-63.2456059437692, 18.283210111463976], [-63.236034820451465, 18.282991602727954], [-63.252030887180354, 18.272969970560457]]], [[[-63.1845043622015, 18.26804736437459], [-63.179493546117754, 18.266505574810356], [-63.18354074372386, 18.265156508941654], [-63.18700977024338, 18.265349232637185], [-63.18970790198078, 18.267469193288], [-63.19221331002265, 18.268818259156703], [-63.190093349371836, 18.26920370654776], [-63.18643159915679, 18.268432811765646], [-63.1845043622015, 18.26804736437459]]], [[[-63.174290006338474, 18.26457833785507], [-63.172362769383184, 18.26457833785507], [-63.171206427210016, 18.265541956332715], [-63.16908646655919, 18.265349232637185], [-63.16831557177708, 18.261880206117667], [-63.17139915090554, 18.26149475872661], [-63.17409728264295, 18.262072929813193], [-63.174290006338474, 18.26457833785507]]]], "type": "MultiPolygon"}',
  '{"coordinates": [[[-4.612131313999953, 54.05695221600007], [-4.620757615999935, 54.069647528000075], [-4.631255662999934, 54.07062409100007], [-4.64321855399993, 54.06659577000005], [-4.656402147999927, 54.063788153000075], [-4.671009894999941, 54.06704336100006], [-4.68968665299991, 54.08152903900003], [-4.704497850999928, 54.084865627000056], [-4.725982225999928, 54.079006252000056], [-4.745594855999911, 54.06736888200004], [-4.766224738999938, 54.05923086100006], [-4.790150519999941, 54.063788153000075], [-4.790150519999941, 54.07123444200005], [-4.784820115999935, 54.07306549700007], [-4.781320766999954, 54.07477448100008], [-4.777088995999918, 54.076239325000074], [-4.769683397999927, 54.077460028000075], [-4.770863410999937, 54.09682851800005], [-4.737172003999945, 54.12490469000005], [-4.728138800999943, 54.142564195000034], [-4.727650519999941, 54.17259349200003], [-4.723784959999932, 54.18545156500005], [-4.714466925999943, 54.20034414300005], [-4.711089647999927, 54.20990631700005], [-4.712269660999937, 54.21662018400008], [-4.710275844999956, 54.22064850500004], [-4.68814042899993, 54.22378164300005], [-4.663970506999931, 54.23354726800005], [-4.643055792999917, 54.24941640800006], [-4.612456834999932, 54.26544830900008], [-4.598378058999913, 54.276597398000035], [-4.554595506999931, 54.339178778000075], [-4.530995245999918, 54.366115627000056], [-4.495350714999915, 54.38589101800005], [-4.442005988999938, 54.404364325000074], [-4.404449022999927, 54.40965403900003], [-4.385731574999909, 54.415716864000046], [-4.367787238999938, 54.41901276200008], [-4.351307745999918, 54.41380442900004], [-4.357085740999935, 54.39940013200004], [-4.377552863999938, 54.36188385600008], [-4.375599738999938, 54.34833405200004], [-4.370228644999941, 54.34088776200008], [-4.363758917999917, 54.324286200000074], [-4.358225063999953, 54.317613023000035], [-4.350493943999936, 54.31476471600007], [-4.332183397999927, 54.31342194200005], [-4.324696417999917, 54.310207424000055], [-4.319406704999949, 54.30459219000005], [-4.313221808999913, 54.29559967700004], [-4.311919725999928, 54.287176825000074], [-4.32095292899993, 54.28351471600007], [-4.326730923999946, 54.28034088700008], [-4.340809699999909, 54.263006903000075], [-4.348255988999938, 54.25950755400004], [-4.354359503999945, 54.25153229400007], [-4.359771287999934, 54.242254950000074], [-4.365630662999934, 54.23505280200004], [-4.371815558999913, 54.23126862200007], [-4.399159308999913, 54.22207265800006], [-4.390777147999927, 54.19472890800006], [-4.412587042999917, 54.18008047100005], [-4.446888800999943, 54.169134833000044], [-4.475493943999936, 54.15314362200007], [-4.474598761999914, 54.14081452000005], [-4.529286261999914, 54.11908600500004], [-4.543771938999953, 54.10846588700008], [-4.549794074999909, 54.10065338700008], [-4.563832160999937, 54.09682851800005], [-4.579579230999911, 54.09442780200004], [-4.590931769999941, 54.091050523000035], [-4.598947719999956, 54.08388906500005], [-4.612131313999953, 54.05695221600007]]], "type": "Polygon"}']}
pandas 数据帧 geojson geopandas

评论


答:

1赞 Timeless 11/15/2023 #1

您可以将形状应用于每个元素并将其传递给 GeoDataFramegeometry

from shapely.geometry import shape

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

如果列包含类似 geojson 的字符串,则需要使用以下命令:Geo Shape

from ast import literal_eval

geom = df_uk.pop("Geo Shape").apply(lambda x: shape(literal_eval(x)))

gdf = gpd.GeoDataFrame(df_uk, geometry=geom) # with optional `crs` (see below)

您可能还需要添加 CRS : 。gdf = gpd.GeoDataFrame(..., crs="EPSG:4326")

enter image description here

输出:

print(gdf)

        SOVEREIGNT       NAME                                           geometry
35  United Kingdom  Gibraltar  POLYGON ((-5.35839 36.14111, -5.33877 36.14112...

# <class 'geopandas.geodataframe.GeoDataFrame'>

使用的输入:

df_uk = pd.DataFrame(
    {'SOVEREIGNT': ['United Kingdom'], 'NAME': ['Gibraltar'],
     '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'}]}, index=[35])

评论

0赞 boathon 11/15/2023
谢谢。在您的示例中,它工作正常,但是当我在整个数据帧上使用它时,我收到以下错误消息:AttributeError: 'str' object has no attribute 'get'
0赞 Timeless 11/15/2023
你能吗?print(type(df_uk.at[0, "Geo Shape"]))
0赞 boathon 11/15/2023
我有<类'str'>
0赞 Timeless 11/15/2023
你能在制作前添加这两行吗? ?gdffrom ast import literal_eval; df_uk["Geo Shape"] = df_uk["Geo Shape"].apply(literal_eval).str[0]
1赞 boathon 11/15/2023
工作正常,非常感谢!