简体   繁体   English

复杂的 json 到 Pandas 数据框

[英]Complex json to pandas dataframe

There are lots of question about json to pandas dataframe but none of them solved my issues.有很多关于 json to pandas 数据框的问题,但没有一个能解决我的问题。 I am practicing on this complex json file which looks like this我正在练习这个看起来像这样的复杂 json 文件

{
  "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.我想使用pd.read_json()将此 json 直接转换为pd.read_json()数据帧,我的主要目标是提取 Id、坐标和时间戳pd.read_json() As this is very complex json, normal way of pd.read_json() , simply doesnt give correct output.由于这是非常复杂的 json, pd.read_json()正常方式,根本没有给出正确的输出。 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.您可以读取 json 以将其加载到字典中。 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:您可以直接读取 json,然后将features数组作为 dict features给 Pandas,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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