2016-11-21 75 views
0

我用Google Maps API获取美国国家公园的数据。我试图将这些数据归一化为一个熊猫数据框。正常化google map api json to pandas df

但是这是行不通的: table = pd.io.json.json_normalize(park_json) 并产生像这样的表: enter image description here

我最初试图正常化只有结果列,但它给出了这样的错误:类型错误:列表索引必须为整数或切片,而不是str

然后我尝试:`new_table = pd.read_json((table ['results'])。to_json(),orient ='index')' 但它错误:ValueError:数组必须全部相同长度

有什么建议吗?谢谢!!!

例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}}}}]}

+0

你想要哪些数据? 'geometry'或者只是'address_components'? –

+0

的几何图,谢谢! –

回答

1

有了正确的结构化JSON,你可以这样做:

import json 
import pandas 

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

顶级JSON属性名称将在列0中第一列相应的值。这也适用于嵌套在原始对象内的任何json对象。我无法让它与你的json示例一起工作,但它可以在这里使用第一个示例json:http://jsonapi.org/examples/