2016-11-21 31 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/