Python 从嵌套的json文件中展开并构造新列
我正在使用Python3.7.0,目前面临一个无法找到解决方案的问题。考虑API中的下列单个数据项:Python 从嵌套的json文件中展开并构造新列,python,json,pandas,Python,Json,Pandas,我正在使用Python3.7.0,目前面临一个无法找到解决方案的问题。考虑API中的下列单个数据项: data = {'publications': [{'title': 'The effect of land‐use changes on the hydrological behaviour of Histic Andosols in south Ecuador', 'author_affiliations': [[{'first_name': 'W.', 'last_nam
data = {'publications': [{'title': 'The effect of land‐use changes on the hydrological behaviour of Histic Andosols in south Ecuador',
'author_affiliations': [[{'first_name': 'W.',
'last_name': 'Buytaert',
'researcher_id': 'ur.01136506420.02',
'affiliations': [{'id': 'grid.442123.2',
'name': 'University of Cuenca',
'org_types': ['Education'],
'city': 'Cuenca',
'city_id': 3658666,
'country': 'Ecuador',
'country_code': 'EC',
'state': None,
'state_code': None},
{'id': 'grid.5596.f',
'name': 'KU Leuven',
'org_types': ['Education'],
'city': 'Leuven',
'city_id': 2792482,
'country': 'Belgium',
'country_code': 'BE',
'state': None,
'state_code': None}]},
{'first_name': 'G.',
'last_name': 'Wyseure',
'researcher_id': 'ur.012246446667.91',
'affiliations': [{'id': 'grid.5596.f',
'name': 'KU Leuven',
'org_types': ['Education'],
'city': 'Leuven',
'city_id': 2792482,
'country': 'Belgium',
'country_code': 'BE',
'state': None,
'state_code': None}]},
{'first_name': 'B.',
'last_name': 'De Bièvre',
'researcher_id': 'ur.013305075217.11',
'affiliations': [{'id': 'grid.442123.2',
'name': 'University of Cuenca',
'org_types': ['Education'],
'city': 'Cuenca',
'city_id': 3658666,
'country': 'Ecuador',
'country_code': 'EC',
'state': None,
'state_code': None}]},
{'first_name': 'J.',
'last_name': 'Deckers',
'researcher_id': 'ur.0761456127.40',
'affiliations': [{'id': 'grid.5596.f',
'name': 'KU Leuven',
'org_types': ['Education'],
'city': 'Leuven',
'city_id': 2792482,
'country': 'Belgium',
'country_code': 'BE',
'state': None,
'state_code': None}]}]],
'FOR': [{'id': '2539',
'name': '0406 Physical Geography and Environmental Geoscience'}],
'issn': ['0885-6087', '1099-1085'],
'journal': {'id': 'jour.1043737', 'title': 'Hydrological Processes'},
'type': 'article',
'research_org_country_names': ['Belgium', 'Ecuador'],
'doi': '10.1002/hyp.5867',
'year': 2005,
'times_cited': 72}],
'_stats': {'total_count': 957, 'limit': 1, 'offset': 0}}
我的目标是构建一个数据帧,在该数据帧中嵌套的字典最终组合在一起(用逗号分隔),在其他情况下,使用更复杂的组合。为了给出一个想法,我正在寻找以下结构:
在“作者隶属关系”专栏中,这是最棘手的一个。考虑到我上面提到的条目,对于第一作者来说,这应该像“W.Buyteart(厄瓜多尔的Cuenca大学;比利时的KU Leuven大学)”等等
到目前为止,我的尝试惨遭失败。我得到的最接近的是这个非常幼稚的代码:
from pandas.io.json import json_normalize
data = data['publications']
df = json_normalize(data)
我知道有很多问题和我一样。然而,我还没有发现类似的东西(或者至少我没有很容易地注意到)。我感谢你的评论和帮助
编辑
正如评论中所建议的,我将所需的输出作为文本:
FOR | author_affiliations |doi | issn | journal.id | journal.title | countries | times_cited | title | type | year
0406 Physical... | W. Buytaert (University of Cuenca, Ecuador;KU Leuven, Belgium), ... | 10.1002/hyp.5867| 0885-6087,1099-1085 | jour.1043737 | Hydrological Processes | Belgium,Ecuador | 72 | The effect ... | article | 2005
尝试使用
nested\u to\u record
,然后转换为pandas数据帧,然后手动更改列:
from pandas.io import json
data = data['publications']
df = json.nested_to_record(data)
df=pd.DataFrame(df)
df['FOR']=df['FOR'].tolist()[0][0]['name']
df['author_affiliations']=','.join([i[0]['first_name']+i[0]['last_name']+' ('+i[0]['affiliations'][0]['name']+', '+i[0]['affiliations'][0]['country']+';'+i[0]['affiliations'][1]['name']+', '+i[0]['affiliations'][1]['country'] for i in df['author_affiliations'][0]])
df['issn']=','.join(df['issn'][0])
df['research_org_country_names']=','.join(df['research_org_country_names'][0])
现在:
print(df)
是(显示为图像,jupyter笔记本结果,因为对于我的空闲来说太大):
注意:
json.nested_to_record
产生错误,请改为json.json_normalize
请以文本而不是图像的形式提供所需的输出。我在0.23.3中找不到json.nested_to_record
?您使用的是哪个版本。'0.19.2'
versionSo,我们需要寻找替代的json\u normalize
:-)谢谢您的回答。这只适用于一个条目,但是一个包含多个条目的json文件呢?这部分可能会发生什么变化?df['author\u affiliations']=',加入([i['first\u name']+i['last\u name']df['author\u affiliations']][0][0]])