【问题标题】:Json in python rename, deletepython中的Json重命名,删除
【发布时间】:2021-11-10 08:20:54
【问题描述】:

我使用这种结构处理大型 geojson 数据(超过 1 Gb)。 是其中的一部分。

{'type': 'FeatureCollection',
 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}},
 'features': [{'type': 'Feature',
   'properties': {'date_create': '15.03.2008',
    'statecd': '06',
    'cc_date_approval': None,
    'children': None,
    'adate': '23.08.2017',
    'cc_date_entering': '01.01.2014',
    'rifr_cnt': None,
    'parcel_build_attrs': None,
    'rifr': None,
    'sale_date': None,
    'area_unit': '055',
    'util_code': None,
    'util_by_doc': None,
    'area_value': 115558.0,
    'application_date': None,
    'sale': None,
    'cad_unit': '383',
    'kvartal': '69:3:11',
    'parent_id': '69:3:11:248',
    'sale_cnt': None,
    'sale_doc_date': None,
    'date_cost': None,
    'category_type': '003008000000',
    'rifr_dep': None,
    'kvartal_cn': '69:03:0000011',
    'parent_cn': '69:03:0000011:248',
    'cn': '69:03:0000011:245',
    'is_big': False,
    'rifr_dep_info': None,
    'sale_dep': None,
    'sale_dep_uo': None,
    'parcel_build': False,
    'id': '69:3:11:245',
    'address': '',
    'area_type': '009',
    'parcel_type': 'parcel',
    'sale_doc_num': None,
    'sale_doc_type': None,
    'sale_price': None,
    'cad_cost': 139698.06,
    'fp': None,
    'center': {'x': 33.14727379331379, 'y': 55.87764081906541}},
   'geometry': {'type': 'MultiPolygon',
    'coordinates': []},

我需要保存特征 'id' 和 'area_value' 重命名它们并删除其他的,以便嵌套表内只有这两个键。

而且我必须保存其他结构的数据,否则程序将无法理解它们。

我只能检索数据,但不能重写它们。 我使用这种方法。 对于 pandas,我有 pd.Dataframe,我知道如何过滤和选择,但我不知道返回或重写数据。

from pandas.io.json import json_normalize

f = 'data_file_name.json'

with open(f,'r') as dff:
    data = json.loads(dff.read())
    
df = json_normalize(data,record_path=['features'], errors='ignore')
df

另外,我尝试使用 ijson。在这里我也有同样的问题

def parse_json(json_filename):
    with open(json_filename, 'rb') as input_file:
        # load json iteratively
        parser = ijson.parse(input_file)
        for prefix, event, value in parser:
            if prefix == 'features.item.properties.id':
                id_val = value
            if prefix == 'features.item.properties.area_value':
                area_val = value
print(id_val)

#             print('prefix={}, event={}, value={}'.format(pref ix, event, value))

            
            
if __name__ == '__main__':
    parse_json('data_file_name.json')
    

谢谢大家!

【问题讨论】:

  • 加载字典,更新它并用json.dump转储改变的字典。你能举一个预期输出的例子吗?
  • 遗憾的是还没有示例。只是对数据结构的理解

标签: json python-3.x pandas geojson geopandas


【解决方案1】:

如果您确定数据是 GeoJSON 并且结构正确,则此答案有效:

要读取 GeoJSON 数据,您可以使用 Geopandas 库:

import geopandas as gpd

gdf = gpd.read_file('data_file_name.json')

这将在 geopandas GeoDataFrame 中加载 GeoJSON 文件,这是一个具有空间分析功能的 pandas 数据框。 你可以阅读更多here

对数据进行操作后,可以将其导出到 GeoJSON:

gdf.to_file('data_file_name.geojson', driver='GeoJSON')

这将保留 geojson 结构。如果您的进一步分析使用其他软件,您可以将其保存为其他空间格式,例如 Geopackage、shapefile 甚至 CSV 以及 WKT 几何格式。

【讨论】:

    猜你喜欢
    • 2018-03-09
    • 2012-07-28
    • 2018-09-21
    • 2013-06-27
    • 1970-01-01
    • 2020-05-28
    • 2021-12-22
    • 2019-03-24
    • 1970-01-01
    相关资源
    最近更新 更多