Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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_Jupyter - Fatal编程技术网

Python 将嵌套json解包到数据帧中的有效方法

Python 将嵌套json解包到数据帧中的有效方法,python,json,pandas,dataframe,jupyter,Python,Json,Pandas,Dataframe,Jupyter,我有一个嵌套的json,我想把它转换成一个数据帧。我能够用json_normalize进行规范化 然而,数据帧中仍然有json层,我也希望将其解包。我怎样才能用最好的方式来做呢?在我目前正在做的项目中,我可能还要再处理几次这个问题 我的json如下所示 { "data": { "allOpportunityApplication": { "data": [ { "id": "111111111", "opportun

我有一个嵌套的json,我想把它转换成一个数据帧。我能够用json_normalize进行规范化

然而,数据帧中仍然有json层,我也希望将其解包。我怎样才能用最好的方式来做呢?在我目前正在做的项目中,我可能还要再处理几次这个问题

我的json如下所示

{
  "data": {
    "allOpportunityApplication": {
      "data": [
        {
          "id": "111111111",
          "opportunity": {
            "programme": {
              "short_name": "XX"
            }
          },
          "person": {
            "home_lc": {
              "name": "NAME"
            }
          },
          "standards": [
            {
              "constant_name": "constant1",
              "standard_option": {
                "option": "true"
              }
            },
            {
              "constant_name": "constant2",
              "standard_option": {
                "option": "true"
              }
            }
          ]
        }
      ]
    }
  }
}
使用了json规范化

standards_df = json_normalize(
    standard_json['allOpportunityApplication']['data'], 
    record_path=['standards'],
    meta=['id','person','opportunity']
)
这样我就得到了一个数据框,其中包含以下列:
constant\u name
standard\u option
id
person
opportunity
。问题在于数据
standard_选项
person
opportunity
都是json,内部只有一个选项

每列的当前输出和预期输出如下

标准期权 当前,“标准选项”列中的一项如下所示:

{'option': 'true'}  
{'programme': {'short_name': 'XX'}}
{'home_lc': {'name': 'NAME'}}       
我希望它是真实的

人 当前,“person”列中的一个项目如下所示:

{'option': 'true'}  
{'programme': {'short_name': 'XX'}}
{'home_lc': {'name': 'NAME'}}       
我希望它看起来像:
XX

机会 当前,“opportunity”列中的一项如下所示:

{'option': 'true'}  
{'programme': {'short_name': 'XX'}}
{'home_lc': {'name': 'NAME'}}       

我希望它看起来像:
NAME

可能不是最好的方法,但我认为它是有效的

standards_df['person']=(standards_df.loc[:,'person']
.apply(lambda x:x['home\u lc']['name']))
标准_df['opportunity']=(标准_df.loc[:,'opportunity']
.apply(lambda x:x['program']['short_name']))
constant\u name standard\u option.option id person opportunity
0 constant1 true 111111111名称XX
1 constant2 true 111111111名称XX

standard_选项
在我运行您的代码时已经很好了

可能不是最好的方法,但我认为它是有效的

standards_df['person']=(standards_df.loc[:,'person']
.apply(lambda x:x['home\u lc']['name']))
标准_df['opportunity']=(标准_df.loc[:,'opportunity']
.apply(lambda x:x['program']['short_name']))
constant\u name standard\u option.option id person opportunity
0 constant1 true 111111111名称XX
1 constant2 true 111111111名称XX
standard_选项
在我运行您的代码时已经很好了