You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用geopandas/geojson包从GeoJSON文件创建pandas DataFrame

GeoJSON转pandas DataFrame实现方案(无需geopandas/geojson依赖)

实现逻辑

GeoJSON文件解析为Python字典后,核心数据都存在features数组中,每个feature包含属性字段properties和几何字段geometry,只需将这两部分内容提取后即可直接转换为DataFrame。

完整代码(在你已有代码基础上扩展)

import json
import pandas as pd

# 你已有的读文件逻辑
with open('Local_Authority_Districts_(December_2020)_UK_BGC.geojson') as f:
    data = json.load(f)

# 提取要素信息转为行数据
row_list = []
for feature in data["features"]:
    # 复制所有属性字段
    row_data = feature["properties"].copy()
    # 可选:保留几何信息,不需要可以删除下一行
    row_data["geometry"] = feature["geometry"]
    row_list.append(row_data)

# 转换为DataFrame
gdf = pd.DataFrame(row_list)

# 可以打印前几行验证结果
print(gdf.head())

可选扩展操作

  • 不需要几何信息的话,删除row_data["geometry"] = feature["geometry"]行即可得到纯属性表
  • 如需拆分几何信息,可基于geometry的字典结构自定义处理逻辑,示例如下:
# 示例:提取MultiPolygon类型几何的所有坐标点列表
def get_all_coords(geo_obj):
    coords = []
    if geo_obj["type"] == "MultiPolygon":
        for polygon in geo_obj["coordinates"]:
            for ring in polygon:
                coords.extend(ring)
    return coords

# 新增坐标列
gdf["coord_list"] = gdf["geometry"].apply(get_all_coords)

内容的提问来源于stack exchange,提问作者Jody

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:45:03