简体   繁体   中英

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 = pd.io.json.json_normalize(park_json) And produces a table like this: 在此处输入图片说明

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

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: http://jsonapi.org/examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM