JSON to pandas DataFrame

后端 未结 11 1717
离开以前
离开以前 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:21

    You could first import your json data in a Python dictionnary :

    data = json.loads(elevations)
    

    Then modify data on the fly :

    for result in data['results']:
        result[u'lat']=result[u'location'][u'lat']
        result[u'lng']=result[u'location'][u'lng']
        del result[u'location']
    

    Rebuild json string :

    elevations = json.dumps(data)
    

    Finally :

    pd.read_json(elevations)
    

    You can, also, probably avoid to dump data back to a string, I assume Panda can directly create a DataFrame from a dictionnary (I haven't used it since a long time :p)

提交回复
热议问题