【问题标题】:How to loop through nested dictionaries in a JSON如何遍历 JSON 中的嵌套字典
【发布时间】:2019-11-11 16:21:54
【问题描述】:

我有一个 json 文件,它给出了芝加哥社区的多边形。这是表单的一个小样本。

{'type': 'Feature',
 'properties': {'PRI_NEIGH': 'Printers Row',
  'SEC_NEIGH': 'PRINTERS ROW',
  'SHAPE_AREA': 2162137.97139,
  'SHAPE_LEN': 6864.247156},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-87.62760697485339, 41.87437097785366],
    [-87.6275952566332, 41.873861712441126],
    [-87.62756611032259, 41.873091933433905],
    [-87.62755513014902, 41.872801941012725],
    [-87.62754038267386, 41.87230261598636],
    [-87.62752573582432, 41.8718067089444],
    [-87.62751740010017, 41.87152447340544],
    [-87.62749380061304, 41.87053328991345],
    [-87.62748640976544, 41.87022285721281],
    [-87.62747968351987, 41.86986997314866],
    [-87.62746758964467, 41.86923545315858],
    [-87.62746178584428, 41.868930955522266]

我想创建一个数据框,其中我有每个“SEC_NEIGH”,链接到这样的坐标

df['SEC_NEIGH'] = 'coordinates'

我尝试使用 for 循环遍历字典,但是当我这样做时,数据框只显示一个“_”

df = {}
for item in data:
    if 'features' in item:
        if 'properties' in item:
            nn = item.get("properties").get("PRI_NEIGH")
        if 'geometry' in item:
            coords = item.get('geometry').get('coordinates')
            df[nn] = coords

df_n=pd.DataFrame(df)

我期待每列都是一个单独的邻域,只有一个值,即坐标列表。相反,我的数据框输出为单个下划线('_')。我的 for 循环有问题吗?

【问题讨论】:

    标签: python json for-loop data-science


    【解决方案1】:

    试试这个:

    import pandas as pd
    data=[
    {'type': 'Feature',
     'properties': {'PRI_NEIGH': 'Printers Row',
      'SEC_NEIGH': 'PRINTERS ROW',
      'SHAPE_AREA': 2162137.97139,
      'SHAPE_LEN': 6864.247156},
     'geometry': {'type': 'Polygon',
      'coordinates': [[-87.62760697485339, 41.87437097785366],
        [-87.6275952566332, 41.873861712441126],
        [-87.62756611032259, 41.873091933433905],
        [-87.62755513014902, 41.872801941012725],
        [-87.62754038267386, 41.87230261598636],
        [-87.62752573582432, 41.8718067089444],
        [-87.62751740010017, 41.87152447340544],
        [-87.62749380061304, 41.87053328991345],
        [-87.62748640976544, 41.87022285721281],
        [-87.62747968351987, 41.86986997314866],
        [-87.62746758964467, 41.86923545315858],
        [-87.62746178584428, 41.868930955522266]]
                  }}
          ]
    
    df = {}
    for item in data:
        if  item["type"] =='Feature':
            if 'properties' in item.keys():
                nn = item.get("properties").get("PRI_NEIGH")
            if 'geometry' in item:
                coords = item.get('geometry').get('coordinates')
                df[nn] = coords
    df_n=pd.DataFrame(df)
    print(df_n)
    

    输出:

                                   Printers Row
    0    [-87.62760697485339, 41.87437097785366]
    1    [-87.6275952566332, 41.873861712441126]
    2   [-87.62756611032259, 41.873091933433905]
    3   [-87.62755513014902, 41.872801941012725]
    4    [-87.62754038267386, 41.87230261598636]
    5     [-87.62752573582432, 41.8718067089444]
    6    [-87.62751740010017, 41.87152447340544]
    7    [-87.62749380061304, 41.87053328991345]
    8    [-87.62748640976544, 41.87022285721281]
    9    [-87.62747968351987, 41.86986997314866]
    10   [-87.62746758964467, 41.86923545315858]
    11  [-87.62746178584428, 41.868930955522266]
    

    【讨论】:

    • 我运行该代码并得到一个错误 ```` ------------------------------ --------------------------------------------- TypeError Traceback(最近最后调用) in 1 df = {} 2 for item in data: ----> 3 if item["type"] =='Feature': 4 if 'properties ' in item.keys(): 5 nn = item.get("properties").get("PRI_NEIGH") TypeError: string indices must be integers ````
    • @Adithyan 请在这里试试repl.it/repls/EagerEuphoricShelfware 没有错误
    • 也许我最初的问题并不清楚。 json 文件实际上比我显示的要大得多。这实际上是我所展示的,为每个社区重复。我认为这就是错误的来源。我不确定我是否有任何意义,但如果有帮助,我很乐意与您分享文件!
    • @Adithyan 我会帮助你的
    • 非常感谢。这是包含该文件的 github 链接。 github.com/adithyansubramanian/Springboard_Capstone_project_1/…如果您无法打开文件,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2017-07-25
    相关资源
    最近更新 更多