繁体   English   中英

将Google Maps api json标准化为pandas df

[英]normalize google maps api json to pandas df

我使用了Google Maps API来获取美国国家公园的数据。 我正在尝试将此数据标准化为pandas数据框。

但是,这不起作用: table = pd.io.json.json_normalize(park_json)并生成如下表: 在此处输入图片说明

我最初尝试仅对结果列进行规范化,但会出现此错误:TypeError:列表索引必须为整数或切片,而不是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}}}}]} {'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}}}}]}

使用结构正确的json,您可以执行以下操作:

import json
import pandas

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

顶级json属性名称将在第0列中,而相应的值将在第1列中。这也适用于嵌套在原始对象中的任何json对象。 我无法将其与您的json样本一起使用,但可以在此处与第一个示例json一起使用: http : //jsonapi.org/examples/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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