如何遍历JSON中的嵌套字典

时间:2019-06-30 22:14:03

标签: python json for-loop data-science

我有一个json文件,该文件提供了芝加哥街区的多边形。这是表格的小样本。

PS> '`n'
`n
PS> "`n"



PS> (Get-Content -path $filename -raw) -replace "`n","`n`n" | 
      Set-Content -path $filename -nonewline

我想创建一个数据框,其中每个“ SEC_NEIGH”都链接到坐标,例如

{'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]

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

df['SEC_NEIGH'] = 'coordinates'

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

1 个答案:

答案 0 :(得分:0)

尝试:

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]