gpt4 book ai didi

Python 用 Pandas 压平多重嵌套字典 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:29 25 4
gpt4 key购买 nike

我正在处理一个 JSON 响应,其格式类似于下面的多嵌套字典:

{u'addresses': [],
u'application_ids': [20855193],
u'applications': [{u'answers': [{u'answer': u'Indeed ',
u'question': u'How did you hear?'}],
u'applied_at': u'2015-10-29T22:19:04.925Z',
u'candidate_id': 9999999,
u'credited_to': None,
u'current_stage': {u'id': 9999999,
u'name': u'Application Review'},
u'id': 9999999,
u'jobs': [{u'id': 9999999,u'name': u'ENGINEER'}],
u'last_activity_at': u'2015-10-29T22:19:04.767Z',
u'prospect': False,
u'rejected_at': None,
u'rejection_details': None,
u'rejection_reason': None,
u'source': {u'id': 7, u'public_name': u'Indeed'},
u'status': u'active'}],
u'attachments': [{u'filename': u'Jason_Bourne.pdf',
u'type': u'resume',
u'url': u'https://resumeURL'}],
u'company': None,
u'coordinator': {u'employee_id': None,
u'id': 9999999,
u'name': u'Batman_Robin'},
u'email_addresses': [{u'type': u'personal',
u'value': u'jasonbourne@gmail.com'}],
u'first_name': u'Jason',
u'id': 9999999,
u'last_activity': u'2015-10-29T22:19:04.767Z',
u'last_name': u'Bourne',
u'website_addresses': []}

我正在尝试将 JSON 扁平化为一个表,并在 pandas 文档中找到了以下示例:

http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.io.json.json_normalize.html

据我了解,“record_path”参数指定了您感兴趣的最低级别记录的路径。“record_path”参数只能是一个字符串,或者字符串列表。但是,要在上面的数据中调用“答案”记录,我必须按如下方式指定字符串 索引;

answer = data['applications'][0]['answers']['answer']
question = data['applications'][0]['answers']['question']

如何输入上面的记录路径作为 json_normalize 函数的参数?

谢谢!

最佳答案

我认为你可以使用 record_path 嵌套 list:

from pandas.io.json import json_normalize    
df = json_normalize(d, ['applications', ['answers']])
print (df)
answer question
0 Indeed How did you hear?

关于Python 用 Pandas 压平多重嵌套字典 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com