Json_normalise 从嵌套 API 到 DF 抛出类型错误

Json_normalise from nested API to DF throwing type error

我正在尝试使用具有以下结构的 api 并将其加载到 pandas DF 中,每个项目 ID 一行,例如2、6 和每个条目的高、高时间、低和低时间列。

{
"data": {
    "2": {
        "high": 142,
        "highTime": 1617214328,
        "low": 140,
        "lowTime": 1617214323
    },
    "6": {
        "high": 182198,
        "highTime": 1617214063,
        "low": 182198,
        "lowTime": 1617214137

到目前为止,我一直在 json 响应中使用 json_normalise,它为每个条目加载包含多个嵌套列的一行:

data.2.high | data.2.highTime | data.2.low | data.2.lowTime etc

结果,我尝试为 'data' 添加 record_path,认为这可以解决它是嵌套列表的事实,但这样做会抛出

 raise TypeError(
257                     f"{js} has non list value {result} for path {spec}. "
258                     "Must be list or null."

我认为那是因为我的 res['data'] 类型是一个字典,而不是一个列表本身,但我有点困惑如何解决这个问题,或者这是否正确。

如有任何帮助,我们将不胜感激

TL;DR

只需使用

df = pd.DataFrame.from_records(json_data['data']).T.reset_index()

说明

在您的场景中,pandas from_recordsjson_normalise 效果更好。出现这种情况是因为您的响应的结构是 ID 是键而不是值。

例如,对于这个响应示例,其中有一个键 id 及其对应的值

json_data={
"data": [{
        "id":2,
        "high": 142,
        "highTime": 1617214328,
        "low": 140,
        "lowTime": 1617214323
    },{
        "id":6,
        "high": 182198,
        "highTime": 1617214063,
        "low": 182198,
        "lowTime": 1617214137
    }]}

json_normalize 配合使用效果很好,如下所示。

pd.json_normalize(json_data['data'])
id  high    highTime    low     lowTime
2   142     1617214328  140     1617214323
6   182198  1617214063  182198  1617214137

但是,您的 JSON 响应包含 ID 作为键,

json_data={
"data": {
    "2": {
        "high": 142,
        "highTime": 1617214328,
        "low": 140,
        "lowTime": 1617214323
    },
    "6": {
        "high": 182198,
        "highTime": 1617214063,
        "low": 182198,
        "lowTime": 1617214137
    }}}

所以 from_records 效果更好。

df = pd.DataFrame.from_records(json_data['data']).T.reset_index()
index   high    highTime    low     lowTime
2       142     1617214328  140     1617214323
6       182198  1617214063  182198  1617214137

此外,事情没有奏效,因为您可能传递了完整的 json 响应 json_data 而不是通过数据键 json_data['data'].

选择