熊猫json规范化包含对象数组的对象属性

时间:2020-04-17 15:16:08

标签: python pandas dataframe

如果我的json数据格式如下:

{
  "result": [
    {
      "id": 878787,
      "name": "Testing",
      "schema": {
        "id": 3463463,
        "smartElements": [
          {
            "svKey": "Model",
            "value": {
              "type": "type1",
              "value": "ThisValue"
            }
          },
          {
            "svKey": "SecondKey",
            "value": {
              "type": "example",
              "value": "ThisValue2"
            }
          }
        ]
      }
    },
    {
      "id": 333,
      "name": "NameName",
      "schema": {
        "id": 1111,
        "smartElements": [
          {
            "svKey": "Model",
            "value": {
              "type": "type1",
              "value": "NewValue"
            }
          },
          {
            "svKey": "SecondKey",
            "value": {
              "type": "example",
              "value": "ValueIs"
            }
          }
        ]
      }
    }
  ]
}

有一种规范化它的方法,所以我得到记录:

name          Model          SecondKey        
Testing       ThisValue      ThisValue2
NameName      NewValue       ValueIs

我可以将smartElements应用于熊猫系列,但无法找到将 smartElements[x].svKey 分解为列标题和 smartElements[x].value.value < / strong>到该列的值和/或合并它。

2 个答案:

答案 0 :(得分:3)

我将跳过尝试使用预先烘焙的解决方案,而自己浏览json

import json
import pandas as pd

data = json.load(open('my.json'))

records = []
for d in data['result']:
    record = {}
    record['name'] = d['name']
    for ele in d['schema']['smartElements']:
        record[ele['svKey']] = ele['value']['value']
    records.append(record)

pd.DataFrame(records)

       name      Model   SecondKey
0   Testing  ThisValue  ThisValue2
1  NameName   NewValue     ValueIs

答案 1 :(得分:1)

我的解决方案

import pandas as pd
import json

with open('test.json') as f:
   a = json.load(f)
d = pd.json_normalize(data=a['result'], errors='ignore', record_path=['schema', 'smartElements'], meta=['name'])

print(d)

产生

       svKey value.type value.value      name
0      Model      type1   ThisValue   Testing
1  SecondKey    example  ThisValue2   Testing
2      Model      type1    NewValue  NameName
3  SecondKey    example     ValueIs  NameName