0

I tried to read this json file:

'https://www.redistricting.state.pa.us/Resources/GISData/Districts/Legislative/House/2021-Final/JSON/2022%20LRC-House-Final.json'

from urllib.request import urlopen
import json
with urlopen('https://www.redistricting.state.pa.us/Resources/GISData/Districts/Legislative/House/2021-Final/JSON/2022%20LRC-House-Final.json') as response:
    level_1=json.load(response)

However, I cannot create dataframe from it using:

pd.DataFrame(level_1)

I get error:

ValueError: All arrays must be of the same length

How can I solve the problem?

1 Answer 1

1

You could try this and then massage the data to your liking:

import requests
import pandas as pd

api_url = 'https://www.redistricting.state.pa.us/Resources/GISData/Districts/Legislative/House/2021-Final/JSON/2022%20LRC-House-Final.json'

print(pd.json_normalize(requests.get(api_url).json()).T)

Output:

                                                                0
name                                                    Districts
map_layer_type                                               Area
bounds          [[-80.661781, 39.651554], [-74.547303, 42.5843...
center                                    [-77.604542, 41.117936]
zoom                                                            8
median_zoom                                                    10
count                                                         203
property_names  [Area, District, Members, Locked, Name, Adj_Po...
type                                            FeatureCollection
features        [{'type': 'Feature', 'id': 1, 'properties': {'...

If you want features then why not try this?

import requests
import pandas as pd

api_url = 'https://www.redistricting.state.pa.us/Resources/GISData/Districts/Legislative/House/2021-Final/JSON/2022%20LRC-House-Final.json'

print(pd.DataFrame(requests.get(api_url).json()["features"]))
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for not making it clear from the beginning, but I am interested in 'features' row's data. I want to make df from this row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.