JSON to pandas DataFrame

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

    Once you have the flattened DataFrame obtained by the accepted answer, you can make the columns a MultiIndex ("fancy multiline header") like this:

    df.columns = pd.MultiIndex.from_tuples([tuple(c.split('.')) for c in df.columns])
    
    0 讨论(0)
  • 2020-11-22 06:32

    Here is small utility class that converts JSON to DataFrame and back: Hope you find this helpful.

    # -*- coding: utf-8 -*-
    from pandas.io.json import json_normalize
    
    class DFConverter:
    
        #Converts the input JSON to a DataFrame
        def convertToDF(self,dfJSON):
            return(json_normalize(dfJSON))
    
        #Converts the input DataFrame to JSON 
        def convertToJSON(self, df):
            resultJSON = df.to_json(orient='records')
            return(resultJSON)
    
    0 讨论(0)
  • 2020-11-22 06:35

    Check this snip out.

    # 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 dataframe
    train = pd.DataFrame.from_dict(dict_train, orient='index')
    train.reset_index(level=0, inplace=True)
    

    Hope it helps :)

    0 讨论(0)
  • 2020-11-22 06:35

    Just a new version of the accepted answer, as python3.x does not support urllib2

    from requests import request
    import json
    from pandas.io.json import json_normalize
    
    path1 = '42.974049,-81.205203|42.974298,-81.195755'
    response=request(url='http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false', method='get')
    elevations = response.json()
    elevations
    data = json.loads(elevations)
    json_normalize(data['results'])
    
    0 讨论(0)
  • 2020-11-22 06:36
    #Use the small trick to make the data json interpret-able
    #Since your data is not directly interpreted by json.loads()
    
    >>> import json
    >>> f=open("sampledata.txt","r+")
    >>> data = f.read()
    >>> for x in data.split("\n"):
    ...     strlist = "["+x+"]"
    ...     datalist=json.loads(strlist)
    ...     for y in datalist:
    ...             print(type(y))
    ...             print(y)
    ...
    ...
    <type 'dict'>
    {u'0': [[10.8, 36.0], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'1': [[10.8, 36.1], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'2': [[10.8, 36.2], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'3': [[10.8, 36.300000000000004], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'4': [[10.8, 36.4], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'5': [[10.8, 36.5], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'6': [[10.8, 36.6], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'7': [[10.8, 36.7], {u'10': 0, u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'8': [[10.8, 36.800000000000004], {u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    <type 'dict'>
    {u'9': [[10.8, 36.9], {u'1': 0, u'0': 0, u'3': 0, u'2': 0, u'5': 0, u'4': 0, u'7': 0, u'6': 0, u'9': 0, u'8': 0}]}
    
    
    
    0 讨论(0)
提交回复
热议问题