JSON到pandas DataFrame我想要做的是沿著緯度和經度坐標指定的路徑從谷歌地圖API中提取高程數(shù)據(jù),如下所示:from urllib2 import Request, urlopenimport json
path1 = '42.974049,-81.205203|42.974298,-81.195755'request=Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false')response = urlopen(request)elevations = response.read()這給了我一個如下所示的數(shù)據(jù):elevations.splitlines()['{',
' "results" : [',
' {',
' "elevation" : 243.3462677001953,',
' "location" : {',
' "lat" : 42.974049,',
' "lng" : -81.205203',
' },',
' "resolution" : 19.08790397644043',
' },',
' {',
' "elevation" : 244.1318664550781,',
' "location" : {',
' "lat" : 42.974298,',
' "lng" : -81.19575500000001',
' },',
' "resolution" : 19.08790397644043',
' }',
' ],',
' "status" : "OK"',
'}']當我在這里投入DataFrame時,我得到的是:pd.read_json(elevations)這就是我想要的:我不確定這是否可行,但主要是我正在尋找的是一種能夠將高程,緯度和經度數(shù)據(jù)放在一個pandas數(shù)據(jù)幀中的方法(不必有花哨的多線頭)。如果任何人可以幫助或提供一些有關處理這些數(shù)據(jù)的建議,那將是非常好的!如果你不能告訴我之前我沒有用過json數(shù)據(jù)...編輯:這種方法并不是那么有吸引力,但似乎有效:data = json.loads(elevations)lat,lng,el = [],[],[]for result in data['results']:
lat.append(result[u'location'][u'lat'])
lng.append(result[u'location'][u'lng'])
el.append(result[u'elevation'])df = pd.DataFrame([lat,lng,el]).T最終數(shù)據(jù)框具有列緯度,經度,高程
3 回答

互換的青春
TA貢獻1797條經驗 獲得超6個贊
檢查這個剪輯。
# reading the JSON data using json.load()file = 'data.json'with open(file) as train_file: dict_train = json.load(train_file)# converting json dataset from dictionary to dataframetrain = pd.DataFrame.from_dict(dict_train, orient='index')train.reset_index(level=0, inplace=True)
希望能幫助到你 :)
添加回答
舉報
0/150
提交
取消