开发者社区> 问答> 正文

pandas json_normalize所有列都嵌套了字典展平

我有一个从非官方谷歌词典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?

展开
收起
一码平川MACHEL 2019-01-18 10:27:34 7021 0
1 条回答
写回答
取消 提交回答
  • 我认为使用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))

    2019-07-17 23:25:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
中文:即学即用的Pandas入门与时间序列分析 立即下载
即学即用的Pandas入门与时间序列分析 立即下载
低代码开发师(初级)实战教程 立即下载