使用json_normalize展平列表中的双嵌套字典

q0qdq0h2  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(291)
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'}}]}}}

我在列表中有一个嵌套字典,它是另一个嵌套字典的一部分。上面的整个代码片段本身就是列表的一个元素 response .
我尝试使用以下方法将其展平:

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

上述代码的输出为:

我也希望能把字典的内部列表弄平。
有人能帮我吗?我需要迭代 Dataframe 还是有直接可用的方法?
编辑:预期输出如下

u1ehiz5o

u1ehiz5o1#

可以使用递归生成器函数逐行获得完全展平的结果,然后使用 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]

相关问题