JSON to pandas DataFrame

后端 未结 11 1718
离开以前
离开以前 2020-11-22 05:46

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         


        
11条回答
  •  执念已碎
    2020-11-22 06:19

    I found a quick and easy solution to what I wanted using json_normalize() included in pandas 1.01.

    from urllib2 import Request, urlopen
    import json
    
    import pandas as pd    
    
    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)
    df = pd.json_normalize(data['results'])
    

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

提交回复
热议问题