Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

I have a json file that gives the polygons of the neighborhoods of Chicago. Here is a small sample of the form.

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

I want to create a dataframe where I have each 'SEC_NEIGH', linked to the coordinates such that

df['SEC_NEIGH'] = 'coordinates'

I have tried using a for loop to loop through the dictionaries but when I do so, the dataframe comes out with only showing an '_'

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)

I was expecting something where each column would be a separate neighborhood, with only one value, that being the list of coordinates. Instead, my dataframe outputs as a single underscore('_'). Is there something wrong with my for loop?

1 Answer

0 votes
by (41.4k points)

Use the below code:

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)

This code will give the output as:

                               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]

Browse Categories

...