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

q0qdq0h2  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(490)
  1. response[0]={'@type': 'g:Vertex',
  2. '@value': {'id': 'account-2199023262994',
  3. 'label': 'Account',
  4. 'properties': {'account_number': [{'@type': 'g:VertexProperty',
  5. '@value': {'id': {'@type': 'g:Int32',
  6. '@value': 544016139},
  7. 'value': '0002-1990-2326-2994',
  8. 'label': 'account_number'}}],
  9. 'last_name': [{'@type': 'g:VertexProperty',
  10. '@value': {'id': {'@type': 'g:Int32',
  11. '@value': -1616372909},
  12. 'value': 'Law-Yone',
  13. 'label': 'last_name'}}],
  14. 'first_name': [{'@type': 'g:VertexProperty',
  15. '@value': {'id': {'@type': 'g:Int32',
  16. '@value': -451458550},
  17. 'value': 'Eric',
  18. 'label': 'first_name'}}]}}}

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

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

上述代码的输出为:

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

u1ehiz5o

u1ehiz5o1#

可以使用递归生成器函数逐行获得完全展平的结果,然后使用 collections.defaultdict 将帐户id上的行分组。从那里,您可以抓取所需的键来构建最终帐户 pd.DataFrame :

  1. import pandas as pd, collections
  2. 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'}}]}}}
  3. def flatten(d, c = [], p = []):
  4. t, f = [], []
  5. for a, b in d.items():
  6. (t if not isinstance(b, (dict, list)) else f).append((p+[a], b))
  7. if not f:
  8. yield {'.'.join(a):b for a, b in (c+t)}
  9. else:
  10. for a, b in f:
  11. if isinstance(b, dict):
  12. yield from flatten(b, c=c+t, p = a)
  13. else:
  14. for i in b:
  15. yield from flatten(i, c=c+t, p = a)
  16. d = collections.defaultdict(dict)
  17. for i in flatten(data):
  18. d[i['@value.id']].update(i)
  19. 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']]
  20. df = pd.DataFrame([{j:b[k] for k, j in kv} for b in d.values()])

输出:

  1. @type @value.id @value.label ... last_name.value first_name.id first_name.value
  2. 0 g:Vertex account-2199023262994 Account ... Law-Yone -451458550 Eric
  3. [1 rows x 9 columns]
展开查看全部

相关问题