我有一个从非官方谷歌字典 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']}],
现在,字典 j 有三个键。
j.keys() # dict_keys(['word', 'phonetic', 'meaning'])
我主要对含义感兴趣:
j['meaning'].keys() # dict_keys(['adjective', 'noun', 'verb', 'adverb'])
为了获取熊猫数据框,我使用了以下代码:
json_normalize(data=j['meaning'])
这给出了一个只有 4 列的数据框。
在这里,每个词性(形容词、名词等)都必须有“定义”键,“示例”和“同义词”是可选的。
j['meaning']['adjective'][0].keys() # dict_keys(['definition', 'example', 'synonyms'])
如何获取 4 * 3 = 12 列的数据框,列名像adjective_definition, adjective_example, ...., verb_synonyms?
四季花海
相关分类