我有一个从非官方谷歌词典API返回的嵌套字典(json)。
看起来像这样:
{'word': 'slack',
'phonetic': '/slak/',
'meaning': {'adjective': [{'definition': 'Not taut or held tightly in position; loose.',
'example': 'a slack rope',
'synonyms': ['loose',
'limp',
'not taut',
'not tight',
'hanging',
'flapping']},
{'definition': '(of business) characterized by a lack of work or activity; quiet.',
'example': 'business was rather slack'},
{'definition': 'Having or showing laziness or negligence.',
'example': 'slack accounting procedures',
'synonyms': ['lax',
'negligent',
'neglectful',
'remiss',
'careless',
'slapdash',
'slipshod',
'lackadaisical',
'lazy',
'inefficient',
'incompetent',
'inattentive',
'offhand',
'casual',
'disorderly',
'disorganized']},
{'definition': '(of a tide) neither ebbing nor flowing.',
'example': 'soon the water will become slack, and the tide will turn'}],
'noun': [{'definition': 'The part of a rope or line which is not held taut; the loose or unused part.',
'example': 'I picked up the rod and wound in the slack',
'synonyms': ['looseness', 'play', 'give']},
{'definition': 'Casual trousers.'},
{'definition': 'A spell of inactivity or laziness.',
'example': 'he slept deeply, refreshed by a little slack in the daily routine',
'synonyms': ['lull',
'pause',
'respite',
'spell of inactivity',
'interval',
'break',
'hiatus',
'breathing space']}],
'verb': [{'definition': 'Loosen (something, especially a rope).'},
{'definition': 'Decrease or reduce in intensity, quantity, or speed.',
'example': 'the flow of blood slacked off',
'synonyms': ['reduce',
'lessen',
'slacken',
'slow',
'ease off',
'ease up']},
{'definition': 'Work slowly or lazily.',
'example': 'she reprimanded her girls if they were slacking',
'synonyms': ['idle',
'shirk',
'be inactive',
'be lazy',
'be indolent',
'sit back and do nothing',
'waste time',
'lounge about']},
{'definition': 'Slake (lime).'}],
'adverb': [{'definition': 'Loosely.',
'example': 'their heads were hanging slack in attitudes of despair'}]}}
这就是 slack这个词的意思。为了理解这个含义,我们可以谷歌含义或只是使用以下代码:
import numpy as np
import pandas as pd
import json
from pandas.io.json import json_normalize
from io import StringIO
import requests
word = 'slack'
url = 'https://googledictionaryapi.eu-gb.mybluemix.net/?define=' + word
response = requests.get(url)
content = response.content.decode('utf-8') # list of ugly strings
j = json.loads(content) # json list having nested dictionary
j = j[0]
j
现在,字典j有三个键。
j.keys() # dict_keys(['word', 'phonetic', 'meaning'])
我主要对以下含义感兴趣:
j['meaning'].keys() # dict_keys(['adjective', 'noun', 'verb', 'adverb'])
为了获得pandas数据帧,我使用了以下代码:
json_normalize(data=j['meaning'])
这给出了只有4列的数据帧。
在这里,每个词性(形容词,名词等)必须具有'定义'键和'示例','同义词'是可选的。
j'meaning'[0].keys() # dict_keys(['definition', 'example', 'synonyms'])
如何获得有4 * 3 = 12列数据框,与像列名adjective_definition,adjective_example,..., verb_synonyms?
我认为使用json_normalize的record_path参数可以解决您的问题。由于record_path旨在成为json对象或记录列表的单一路径,因此我不得不多次调用json_normalize,然后连接结果以获取包含所需数据的数据帧。您还可以尝试使用record_prefix参数来设置列命名约定。
from pandas.io.json import json_normalize
from io import StringIO
import requests
word = 'slack'
url = 'https://googledictionaryapi.eu-gb.mybluemix.net/?define=' + word
response = requests.get(url)
content = response.content.decode('utf-8') # list of ugly strings
j = json.loads(content) # json list having nested dictionary
j = j[0]
df_adj = json_normalize(data=j['meaning'], record_path=["adjective"], record_prefix="adjective.")
df_verb = json_normalize(data=j['meaning'], record_path=["verb"], record_prefix="verb.")
df_adv = json_normalize(data=j['meaning'], record_path=["adverb"], record_prefix="adverb.")
df_noun = json_normalize(data=j['meaning'], record_path=["noun"], record_prefix="noun.")
df = pd.concat([df_adj, df_verb, df_adv, df_noun], axis=1)
print(df.head(3))
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。