【问题标题】:flatten doubly nested dictionary inside list using json_normalize使用 json_normalize 在列表中展平双重嵌套字典
【发布时间】:2021-09-19 02:20:00
【问题描述】:
response[0]={'@type': 'g:Vertex',
             '@value': {'id': 'account-2199023262994',
                        'label': 'Account',
                        'properties': {'account_number': [{'@type': 'g:VertexProperty',
                                                           '@value': {'id': {'@type': 'g:Int32',
                                                                             '@value': 544016139},
                                                                      'value': '0002-1990-2326-2994',
                                                                      'label': 'account_number'}}],
                                       'last_name': [{'@type': 'g:VertexProperty',
                                                      '@value': {'id': {'@type': 'g:Int32',
                                                                        '@value': -1616372909},
                                                                 'value': 'Law-Yone',
                                                                 'label': 'last_name'}}],
                                       'first_name': [{'@type': 'g:VertexProperty',
                                                       '@value': {'id': {'@type': 'g:Int32',
                                                                         '@value': -451458550},
                                                                  'value': 'Eric',
                                                                  'label': 'first_name'}}]}}}

我在列表中有一个嵌套字典,它是另一个嵌套字典的一部分。上面的整个代码 sn-p 本身就是列表response 的一个元素。

我尝试使用以下方法将其展平:

pd.json_normalize(response, meta = ['@type', ['@value', 'id'], ['@value', 'label'], ['@value', 'properties']])

上述代码的输出是:

我也期望将字典的内部列表变平。

有人可以帮我吗?我需要遍历数据框还是有任何直接可用的方法?

编辑: 预期输出如下

【问题讨论】:

  • @Ajax1234 我已经编辑了问题,请检查预期输出

标签: python pandas dictionary nested json-normalize


【解决方案1】:

您可以使用递归生成器函数逐行获取完整的展平结果,然后使用collections.defaultdict 对帐户 ID 上的行进行分组。从那里,您可以获取所需的密钥来构建您的最终pd.DataFrame

import pandas as pd, collections
data = {'@type': 'g:Vertex', '@value': {'id': 'account-2199023262994', 'label': 'Account', 'properties': {'account_number': [{'@type': 'g:VertexProperty', '@value': {'id': {'@type': 'g:Int32', '@value': 544016139}, 'value': '0002-1990-2326-2994', 'label': 'account_number'}}], 'last_name': [{'@type': 'g:VertexProperty', '@value': {'id': {'@type': 'g:Int32', '@value': -1616372909}, 'value': 'Law-Yone', 'label': 'last_name'}}], 'first_name': [{'@type': 'g:VertexProperty', '@value': {'id': {'@type': 'g:Int32', '@value': -451458550}, 'value': 'Eric', 'label': 'first_name'}}]}}}
def flatten(d, c = [], p = []):
   t, f = [], []
   for a, b in d.items():
      (t if not isinstance(b, (dict, list)) else f).append((p+[a], b))
   if not f:
      yield {'.'.join(a):b for a, b in (c+t)}
   else:
      for a, b in f:
         if isinstance(b, dict):
             yield from flatten(b, c=c+t, p = a)
         else:
             for i in b:
                yield from flatten(i, c=c+t, p = a)

d = collections.defaultdict(dict)
for i in flatten(data):
   d[i['@value.id']].update(i)

kv = [['@type', '@type'], ['@value.id', '@value.id'], ['@value.label', '@value.label'], ['@value.properties.account_number.@value.id.@value', 'account_number.id'], ['@value.properties.account_number.@value.value', 'account_number.value'], ['@value.properties.last_name.@value.id.@value', 'last_name.id'], ['@value.properties.last_name.@value.value', 'last_name.value'], ['@value.properties.first_name.@value.id.@value', 'first_name.id'], ['@value.properties.first_name.@value.value', 'first_name.value']]
df = pd.DataFrame([{j:b[k] for k, j in kv} for b in d.values()])

输出:

      @type              @value.id @value.label  ...  last_name.value first_name.id  first_name.value
0  g:Vertex  account-2199023262994      Account  ...         Law-Yone    -451458550              Eric

[1 rows x 9 columns]

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2019-06-10
    • 1970-01-01
    • 2019-02-12
    • 2022-01-17
    • 2021-12-11
    • 2020-12-24
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多