Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 使用python将JSON提取到数据帧_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 使用python将JSON提取到数据帧

Python 3.x 使用python将JSON提取到数据帧,python-3.x,pandas,Python 3.x,Pandas,我有一个JSON文件,文件结构如下 [json文件及其结构][1] 我试图将所有细节转换成数据帧或表格形式,尝试使用非规范化,但无法得到实际结果 { "body": [{ "_id": { "s": 0, "i": "5ea6c8ee24826b48cc560e1c" },

我有一个JSON文件,文件结构如下

[json文件及其结构][1] 我试图将所有细节转换成数据帧或表格形式,尝试使用非规范化,但无法得到实际结果

                            {

        "body": [{
                "_id": {
                    "s": 0,
                    "i": "5ea6c8ee24826b48cc560e1c"
                },
                "fdfdsfdsf": "V2_1_0",
                "dsd": "INDIA-",
                "sdsd": "df-as-3e-ds",
                "dsd": 123,
                "dsds": [{
                        "dsd": "s_10",
                        "dsds": [{
                                "dsdsd": "OFFICIAL",
                                "dssd": {
                                    "dsds": {
                                        "sdsd": "IND",
                                        "dsads": 0.0
                                    }
                                },
                                "sadsad": [{
                                        "fdsd": "ABC",
                                        "dds": {
                                            "dsd": "INR",
                                            "dfdsfd": -1825.717444
                                        },
                                        "dsss": [{
                                                "id": "A:B",
                                                "dsdsd": "A.B"
                                            }
                                        ]
                                    }, {
                                        "name": "dssadsa",
                                        "sadds": {
                                            "sdsads": "INR",
                                            "dsadsad": 180.831415
                                        },
                                        "xcs": "L:M",
                                                "sds": "L.M"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
    }

此结构嵌套太多,无法直接放入数据帧中。首先,您需要使用ol'
flatte_json
函数。据我所知,此函数不在库中,但请将其保存在某个位置

def flatten_json(nested_json):
    """
        Flatten json object with nested keys into a single level.
        Args:
            nested_json: A nested json object.
        Returns:
            The flattened json object if successful, None otherwise.
    """
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(nested_json)
    return out
将其应用于您的数据:

import json

with open('deeply_nested.json', r) as f: 
    flattened_json = flatten_json(json.load(f))

df = pd.json_normalize(flattened_json)
df.columns

Index(['body_0__id_s', 'body_0__id_i', 'body_0_schemaVersion',
       'body_0_snapUUID', 'body_0_jobUUID', 'body_0_riskSourceID',
       'body_0_scenarioSets_0_scenario',
       'body_0_scenarioSets_0_modelSet_0_modelPolicyLabel',
       'body_0_scenarioSets_0_modelSet_0_valuation_pv_unit',
       'body_0_scenarioSets_0_modelSet_0_valuation_pv_value',
       'body_0_scenarioSets_0_modelSet_0_measures_0_name',
       'body_0_scenarioSets_0_modelSet_0_measures_0_value_unit',
       'body_0_scenarioSets_0_modelSet_0_measures_0_value_value',
       'body_0_scenarioSets_0_modelSet_0_measures_0_riskFactors_0_id',
       'body_0_scenarioSets_0_modelSet_0_measures_0_riskFactors_0_underlyingRef',
       'body_0_scenarioSets_0_modelSet_0_measures_1_name',
       'body_0_scenarioSets_0_modelSet_0_measures_1_value_unit',
       'body_0_scenarioSets_0_modelSet_0_measures_1_value_value',
       'body_0_scenarioSets_0_modelSet_0_measures_1_riskFactors',
       'body_0_scenarioSets_0_modelSet_0_measures_1_underlyingRef'],
      dtype='object')

请不要附加图像,而是将代码复制到question@Joe:我已复制了jsonShow您所需的输出。。。在图像或任何内容中。@StackKiddy:我添加了输出,我已从pandas.io.json导入模块import json\u normalize,但以AttributeError获取错误消息:模块'pandas.json'没有属性'load',对不起,您应该导入python库json:
import json