Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 复杂的json到数据帧_Python_Json_Pandas_Dataframe - Fatal编程技术网

Python 复杂的json到数据帧

Python 复杂的json到数据帧,python,json,pandas,dataframe,Python,Json,Pandas,Dataframe,关于json到dataframe有很多问题,但没有一个解决了我的问题。我正在练习这个复杂的json文件,它看起来像这样 { "type" : "FeatureCollection", "features" : [ { "Id" : 265068000, "type" : "Feature", "geometry" : { "type" : "Point", "coordinates" : [ 22.170376666666666, 65.5

关于json到dataframe有很多问题,但没有一个解决了我的问题。我正在练习这个复杂的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
    }
  } ]
}
Id,Coordinates,timestampExternal
265068000,[22.170376666666666, 65.57273333333333],1529151039629
265745760,[20.329506666666667, 63.675425000000004],1529151278287
我想使用
pd.read_json()
将这个json直接转换为pandas数据帧,我的主要目标是提取Id、坐标和时间戳。由于这是一个非常复杂的json,正常的
pd.read_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
    }
  } ]
}
Id,Coordinates,timestampExternal
265068000,[22.170376666666666, 65.57273333333333],1529151039629
265745760,[20.329506666666667, 63.675425000000004],1529151278287

您可以直接读取json,然后将
功能
数组作为类似dict的命令提供给熊猫:

代码: 结果:
您可以直接读取json,然后将
功能
数组作为类似dict的命令提供给熊猫:

代码: 结果:
您可以读取json以将其加载到字典中。然后,使用字典理解,提取您想要作为列的属性-

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 

您可以读取json以将其加载到字典中。然后,使用字典理解,提取您想要作为列的属性-

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 

我不知道从计算机的角度看一个结构是如何被认为是复杂的。我不知道从计算机的角度看一个结构是如何被认为是复杂的。谢谢,但是我如何只制作Id、坐标和时间戳外部列,而不是获取所有内容。我将编辑我预期输出的问题,但是如何只生成Id、坐标和时间戳外部列,而不是获取所有内容。我将根据我的期望编辑问题output@user3280146如果答案符合您的目的,您可以接受。@user3280146如果答案符合您的目的,您可以接受答案。