Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 3.x 列表中的嵌套字典到dataframe python_Python 3.x_Dataframe_Json Normalize - Fatal编程技术网

Python 3.x 列表中的嵌套字典到dataframe python

Python 3.x 列表中的嵌套字典到dataframe python,python-3.x,dataframe,json-normalize,Python 3.x,Dataframe,Json Normalize,从api获取json输入: { "api_info": { "status": "healthy" }, "items": [ { "timestamp": "time", "stock_data": [ { "ticker": "stri

从api获取json输入:

{
  "api_info": {
    "status": "healthy"
  },
  "items": [
    {
      "timestamp": "time", 
      "stock_data": [
        {
          "ticker": "string",
          "industry": "string",
          "Description": "string"
        }
      ]
     "ISIN":xxx,
     "update_datetime": "time"
    }
  ]
}
初步运行

apiRawData = requests.get(url).json()['items']
然后运行json_normalize方法:

apiExtractedData = pd.json_normalize(apiRawData,'stock_data',errors='ignore')
这里是初始输出,其中stock_数据仍然包含在列表中。 库存数据在更新日期时间 0[{'description':'zzz','industry':'C','ticker…xxx时间

股票数据 伊辛 更新日期时间 0 [{'description':'zzz','industry':'C','ticker…] 123 时间
我认为您可以使用以下代码将现有数据帧转换为预期数据帧:

apiExtractedData['description'] = apiExtractedData['stock_data'].apply(lambda x: x[0]['description'])
apiExtractedData['industry'] = apiExtractedData['stock_data'].apply(lambda x: x[0]['industry'])
apiExtractedData['ticker'] = apiExtractedData['stock_data'].apply(lambda x: x[0]['ticker'])
然后只需删除股票数据列:

apiExtractedData = apiExtractedData.drop(['stock_data'], axis = 1)

您好,Divyessh,很有效。非常感谢。@BrandonWong您好,如果您觉得答案有帮助,请单击答案左侧的按钮接受:)lol。@Divyessh,记录声誉低于15的人的投票,但不要更改公开显示的帖子分数。