JSON到pandas DataFrame我想要做的是沿着纬度和经度坐标指定的路径从谷歌地图API中提取高程数据,如下所示: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()这给了我一个如下所示的数据: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)这就是我想要的:我不确定这是否可行,但主要是我正在寻找的是一种能够将高程,纬度和经度数据放在一个pandas数据帧中的方法(不必有花哨的多线头)。如果任何人可以帮助或提供一些有关处理这些数据的建议,那将是非常好的!如果你不能告诉我之前我没有用过json数据...编辑:这种方法并不是那么有吸引力,但似乎有效: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最终数据框具有列纬度,经度,高程
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
提交
取消