JSON到 pandas DataFrame [英] JSON to pandas DataFrame

查看:120
本文介绍了JSON到 pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从google maps API提取沿纬度和经度坐标指定的路径的高程数据,如下所示:

  from urllib2 import Request,urlopen 
import 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()

这给了我一个如下所示的数据:

  elevationations.splitlines()

['{',
'results:[',$ b $'''',
'海拔: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',
$}
$ / code>

当我将数据框放入DataFrame时,

  pd.read_json(海拔)

这是我想要的:





我不确定这是否可行,但主要是我所寻找的是一种将高程,纬度和经度数据放在一起的方法在一个熊猫数据框中(不必有花哨的多线头)。



如果有人们可以帮助或提供一些建议,以处理这些数据,这将是非常棒的!如果你不能说我以前用json的数据没有太多工作......



编辑:



这个方法并不是那么有吸引力,但似乎有效:

  data = json.loads(立面图)
lat,lng,el = [],[],[]
用于结果数据['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

结束了包含经度,纬度,高度




我发现了一个快速简单的解决方案,使用最新发布的pandas 0.13中包含的json_normalize函数。

  from urllib2 import请求,urlopen 
从pandas.io.json导入json
导入json_normalize

path1 = '42 .974049,-81.205203 | 42.974298,-81.195755'
request = Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+ )
response = urlopen(请求)
elevation = response.read()
data = json.loads(立面数据)
json_normalize(data [path1 +'& sensor = 'results'])

这给出了一个很好的扁平数据框,其中包含我从google获得的json数据maps API。

What I am trying to do is extract elevation data from a google maps API along a path specified by latitude and longitude coordinates as follows:

from urllib2 import Request, urlopen
import 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()

This gives me a data that looks like this:

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"',
 '}']

when putting into as DataFrame here is what I get:

pd.read_json(elevations)

and here is what I want:

I'm not sure if this is possible, but mainly what I am looking for is a way to be able to put the elevation, latitude and longitude data together in a pandas dataframe (doesn't have to have fancy mutiline headers).

If any one can help or give some advice on working with this data that would be great! If you can't tell I haven't worked much with json data before...

EDIT:

This method isn't all that attractive but seems to work:

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

ends up dataframe having columns latitude, longitude, elevation

解决方案

I found a quick and easy solution to what I wanted using json_normalize function included in the latest release of pandas 0.13.

from urllib2 import Request, urlopen
import json
from pandas.io.json import json_normalize

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()
data = json.loads(elevations)
json_normalize(data['results'])

This gives a nice flattened dataframe with the json data that I got from the google maps API.

这篇关于JSON到 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆