JavaEar 专注于收集分享传播有价值的技术资料

normalize google maps api json to pandas df

I used the google maps api to get data on US National Parks. I am trying to normalize this data into a pandas dataframe.

However this is not working: table = And produces a table like this: enter image description here

I initially tried to normalize only the results column but it gives this error: TypeError: list indices must be integers or slices, not str

I then tried: `new_table = pd.read_json((table['results']).to_json(), orient='index')' but it errors: ValueError: arrays must all be same length

Any suggestions? Thank you!!!

Example json: {'status': 'OK', 'results': [{'formatted_address': 'Acadia National Park, Mt Desert, ME 04660, USA', 'types': ['establishment', 'park', 'point_of_interest'], 'place_id': 'ChIJJSmiDrKjrkwRhFVV_A4i32I', 'address_components': [{'types': ['establishment', 'point_of_interest'], 'long_name': 'Acadia National Park', 'short_name': 'Acadia National Park'}, {'types': ['locality', 'political'], 'long_name': 'Mount Desert', 'short_name': 'Mt Desert'}, {'types': ['administrative_area_level_2', 'political'], 'long_name': 'Hancock County', 'short_name': 'Hancock County'}, {'types': ['administrative_area_level_1', 'political'], 'long_name': 'Maine', 'short_name': 'ME'}, {'types': ['country', 'political'], 'long_name': 'United States', 'short_name': 'US'}, {'types': ['postal_code'], 'long_name': '04660', 'short_name': '04660'}], 'geometry': {'location_type': 'APPROXIMATE', 'location': {'lng': -68.2733346, 'lat': 44.3385559}, 'viewport': {'southwest': {'lng': -68.4344785, 'lat': 44.2350589}, 'northeast': {'lng': -68.1591412, 'lat': 44.40370240000001}}}}]} {'status': 'OK', 'results': [{'formatted_address': 'Adams National Historical Park, 1250 Hancock St, Quincy, MA 02169, USA', 'types': ['establishment', 'museum', 'park', 'point_of_interest'], 'place_id': 'ChIJbbPB5rB844kR7hOzzjBr4Cs', 'address_components': [{'types': ['establishment', 'point_of_interest'], 'long_name': 'Adams National Historical Park', 'short_name': 'Adams National Historical Park'}, {'types': ['street_number'], 'long_name': '1250', 'short_name': '1250'}, {'types': ['route'], 'long_name': 'Hancock Street', 'short_name': 'Hancock St'}, {'types': ['locality', 'political'], 'long_name': 'Quincy', 'short_name': 'Quincy'}, {'types': ['administrative_area_level_2', 'political'], 'long_name': 'Norfolk County', 'short_name': 'Norfolk County'}, {'types': ['administrative_area_level_1', 'political'], 'long_name': 'Massachusetts', 'short_name': 'MA'}, {'types': ['country', 'political'], 'long_name': 'United States', 'short_name': 'US'}, {'types': ['postal_code'], 'long_name': '02169', 'short_name': '02169'}], 'geometry': {'location_type': 'APPROXIMATE', 'location': {'lng': -71.00379099999999, 'lat': 42.252297}, 'viewport': {'southwest': {'lng': -71.00513998029149, 'lat': 42.2509480197085}, 'northeast': {'lng': -71.00244201970848, 'lat': 42.25364598029149}}}}]}


  1. With properly structured json, you can do this:

    import json
    import pandas
    df = pandas.DataFrame(json.load(open('example.json', 'r')).items())

    The top-level json property names will be in column 0 with the corresponding values in column 1. This also works for any json object nested within the original object. I couldn't get it to work with your json sample but it does work with the first example json here: