Python使用Pandas展平多重嵌套字典JSON
我正在处理一个JSON响应,它的格式类似于下面的许多嵌套字典:Python使用Pandas展平多重嵌套字典JSON,python,json,pandas,nested,Python,Json,Pandas,Nested,我正在处理一个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'2
{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文档中找到了以下示例:
据我所知,“record_path”参数指定您感兴趣的最低级别记录的路径。“记录路径”参数只能是字符串或字符串列表。但是,要调用上面数据中的“answers”记录,我必须指定字符串和索引,如下所示
answer = data['applications'][0]['answers']['answer']
question = data['applications'][0]['answers']['question']
如何输入上面的记录路径作为json_normalize函数的参数
谢谢 我认为您可以使用as
记录路径
嵌套列表
:
from pandas.io.json import json_normalize
df = json_normalize(d, ['applications', ['answers']])
print (df)
answer question
0 Indeed How did you hear?
现在我正试图对“当前阶段”下的“名称”做同样的处理。当我键入
df=json\u normalize(d,['applications',['current\u stage','name']])
时,我会在自己的行中找到“Application Review”的每个字母。你可以使用df=json\u normalize(d['applications'][0]['current\u stage]])
,我尝试了很多方法,但不幸的是没有成功。主要问题是[]
在{u'id:9999999,u'name':u'Application Review'}
中缺少[{u'id:9999999,u'name':u'Application Review'}