简体   繁体   中英

Complex json to pandas dataframe

There are lots of question about json to pandas dataframe but none of them solved my issues. I am practicing on this complex json file which looks like this

{
  "type" : "FeatureCollection",
  "features" : [ {
    "Id" : 265068000,
    "type" : "Feature",
    "geometry" : {
      "type" : "Point",
      "coordinates" : [ 22.170376666666666, 65.57273333333333 ]
    },
    "properties" : {
      "timestampExternal" : 1529151039629
    }
  }, {
    "Id" : 265745760,
    "type" : "Feature",
    "geometry" : {
      "type" : "Point",
      "coordinates" : [ 20.329506666666667, 63.675425000000004 ]
    },
    "properties" : {
      "timestampExternal" : 1529151278287
    }
  } ]
}

I want to convert this json directly to pandas dataframe using pd.read_json() My Primary Goal is to extract Id, Coordinates and timestampExternal. As this is very complex json, normal way of pd.read_json() , simply doesnt give correct output. Can you suggest me, how can i approach to solve in this kind of situations. Expected output is something like this

Id,Coordinates,timestampExternal
265068000,[22.170376666666666, 65.57273333333333],1529151039629
265745760,[20.329506666666667, 63.675425000000004],1529151278287

You can read the json to load it into a dictionary. Then, using dictionary comprehension, extract the attributes you want as columns -

import json
import pandas as pd

_json = json.load(open('/path/to/json'))
df_dict = [{'id':item['Id'], 'coordinates':item['geometry']['coordinates'], 
            'timestampExternal':item['properties']['timestampExternal']} for item in _json['features']]

extracted_df = pd.DataFrame(df_dict)

>>>
                               coordinates             id   timestampExternal
0   [22.170376666666666, 65.57273333333333]     265068000   1529151039629
1   [20.329506666666667, 63.675425000000004]    265745760   1529151278287 

You can read the json directly, and then given the features array to pandas as a dict like:

Code:

import json

with open('test.json', 'rU') as f:
    data = json.load(f)

df = pd.DataFrame([dict(id=datum['Id'],
                        coords=datum['geometry']['coordinates'],
                        ts=datum['properties']['timestampExternal'],
                        )
                   for datum in data['features']])
print(df)

Results:

                                     coords         id             ts
0   [22.170376666666666, 65.57273333333333]  265068000  1529151039629
1  [20.329506666666667, 63.675425000000004]  265745760  1529151278287

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM