Pandas 用于读取嵌套的 json 和 json_normalize 或 read_json

Pandas for reading nested json with json_normalize or read_json

我有一个 json object 结构如下:

import pandas as pd
json_data_raw = [{"indicator_value":{"195606": 
                                              {"2010":{"AFG": 0.29, 
                                                       "ZWE": 0.607}, 
                                               "2011": {"AFG": 0.406, 
                                                       "ZWE": 0.737}, 
                                               "2012": {"AFG": 0.345, 
                                                       "ZWE": 0.587}, 
                                               "2013": {"AFG": 0.28, 
                                                       "ZWE": 0.871}, 
                                               "2014": {"AFG": 0.253, 
                                                       "ZWE": 0.88}, 
                                               "2015": {"AFG": 0.262, 
                                                       "ZWE": 0.88}, 
                                               "2016": {"AFG": 0.245, 
                                                       "ZWE": 0.77}, 
                                               "2017": {"AFG": 0.247, 
                                                       "ZWE": 0.845}, 
                                               "2018": {"AFG": 0.254, 
                                                       "ZWE": 0.849}}}, 
                  "country_name": {"AFG": "Afghanistan", 
                                   "ZWE": "Zimbabwe"}, 
                  "indicator_name": {"195606": "Carbon dioxide emissions, production emissions per capita (tonnes)"}}]

当我尝试使用 pd.read_json 方法读取此结构时,我设法仅加载数据的第一个节点,即 "indicator_value""country_name""indicator_name"仅限嵌套实例。

我也尝试过使用 pd.json_normalize 但没有成功,因为我不太了解如何指定参数 record_path 或我的 json object 中包含的元数据(元数据)和得到奇怪的结果。

理想情况下,我想得到这样的 table。

其他 country/year/values...

依此类推

这可能不仅仅是一项任务,而且会涉及其他类型的对象操作。 无论如何,我非常感谢您的帮助。

首先创建一个名为dvariable.....基本上它包含了内部的dictionary

d=json_data_raw[0]

现在创建一个 dataframe df:-

df=pd.DataFrame(list(list(d.values())[0].values())[0])

所以这里 list(list(d.values())[0].values())[0] 更深 3 级 dictionary

现在,

df=df.stack().to_frame()

注意:-这里可以覆盖df或者新建variable 上面的代码创建了一个 multi index dataframe 所以要删除 multi index 我们使用:-

df.reset_index(inplace=True)

现在重命名列:-

df.columns=['country_code','year','Carbon dioxide emssions,production emission per capita(tonnes)']

现在我们将创建一个函数,将 country_code 列更改为国家名称,即

def country(val):
    if val=='AFG':
        return 'Afganistan'
    else:
        return 'Zimbabwe'

现在我们将使用apply()方法

country=df['country_code'].apply(country)

现在我们终于要在 dataframe df

中插入国家/地区列
df.insert(0,'country_name',country)

现在如果你打印 df 你会得到你想要的输出

输出:-

    country_name    country_code    year    Carbon dioxide emssions,production emission per capita(tonnes)
0   Afganistan               AFG    2010    0.290
1   Afganistan               AFG    2011    0.406
2   Afganistan               AFG    2012    0.345
3   Afganistan               AFG    2013    0.280
4   Afganistan               AFG    2014    0.253
5   Afganistan               AFG    2015    0.262
6   Afganistan               AFG    2016    0.245
7   Afganistan               AFG    2017    0.247
8   Afganistan               AFG    2018    0.254
9   Zimbabwe                 ZWE    2010    0.607
10  Zimbabwe                 ZWE    2011    0.737
11  Zimbabwe                 ZWE    2012    0.587
12  Zimbabwe                 ZWE    2013    0.871
13  Zimbabwe                 ZWE    2014    0.880
14  Zimbabwe                 ZWE    2015    0.880
15  Zimbabwe                 ZWE    2016    0.770
16  Zimbabwe                 ZWE    2017    0.845
17  Zimbabwe                 ZWE    2018    0.849

注意:- 好吧,我创建了一个 function 因为在你的 json 对象中只有 2 个国家,如果你有超过 2 个国家并且你有相同的 json 格式然后代码 before I define/create function country() 照原样工作。

但不是制作 function 然后使用 apply() 方法 使用这个:-

countryinfo=list(json_data_raw[0].values())[1]

现在,如果你打印 countryinfo,你会得到一个 dictionary,其中 keys 是国家代码,values 是国家名称

countryinfo 的输出:-

{'AFG': 'Afghanistan', 'ZWE': 'Zimbabwe'}

好的 list(json_data_raw[0].values())[1] 我们正在抓取存储在您的 json object:- json_data_raw 中的 "country_name" 的数据,并将其存储在 variable 命名的国家/地区信息中

所以现在

country=df['country_code'].replace(countryinfo.keys(),countryinfo.values())

所以这里基本上我们将 df['country_code'] 的值替换为 countryinfo dictionary 的值,这样它将 returns 一个 Series 的值,我们正在存储 Seriesvariable 中命名为 country

最后我们 dataframe df

index 0insert 'country_name' 列消失了
df.insert(0,'country_name',country)

现在,如果您打印 df,那么无论您 json_data_raw

中有多少个国家/地区,您都将获得所需的输出

df 的输出:-

    country_name    country_code    year    Carbon dioxide emssions,production emission per capita(tonnes)
0   Afganistan               AFG    2010    0.290
1   Afganistan               AFG    2011    0.406
2   Afganistan               AFG    2012    0.345
3   Afganistan               AFG    2013    0.280
4   Afganistan               AFG    2014    0.253
5   Afganistan               AFG    2015    0.262
6   Afganistan               AFG    2016    0.245
7   Afganistan               AFG    2017    0.247
8   Afganistan               AFG    2018    0.254
9   Zimbabwe                 ZWE    2010    0.607
10  Zimbabwe                 ZWE    2011    0.737
11  Zimbabwe                 ZWE    2012    0.587
12  Zimbabwe                 ZWE    2013    0.871
13  Zimbabwe                 ZWE    2014    0.880
14  Zimbabwe                 ZWE    2015    0.880
15  Zimbabwe                 ZWE    2016    0.770
16  Zimbabwe                 ZWE    2017    0.845
17  Zimbabwe                 ZWE    2018    0.849