第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

pandas json_normalize 所有列都有嵌套的字典扁平化

pandas json_normalize 所有列都有嵌套的字典扁平化

慕妹3242003 2021-10-12 10:45:02
我有一個(gè)從非官方谷歌字典 API 返回的嵌套字典 (json)??雌饋?lái)像這樣:{'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']}],現(xiàn)在,字典 j 有三個(gè)鍵。j.keys() # dict_keys(['word', 'phonetic', 'meaning'])我主要對(duì)含義感興趣:j['meaning'].keys() # dict_keys(['adjective', 'noun', 'verb', 'adverb'])為了獲取熊貓數(shù)據(jù)框,我使用了以下代碼:json_normalize(data=j['meaning'])這給出了一個(gè)只有 4 列的數(shù)據(jù)框。在這里,每個(gè)詞性(形容詞、名詞等)都必須有“定義”鍵,“示例”和“同義詞”是可選的。j['meaning']['adjective'][0].keys() # dict_keys(['definition', 'example', 'synonyms'])如何獲取 4 * 3 = 12 列的數(shù)據(jù)框,列名像adjective_definition, adjective_example, ...., verb_synonyms?
查看完整描述

3 回答

?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊

有點(diǎn)凌亂的解決方案,但我認(rèn)為它有效。從j作為示例字典開始:


res = pd.concat([json_normalize(v, meta=['definition', 'example', 'synonyms']).add_prefix(k + '_')

                 for k, v in j['meaning'].items()], 

                axis=1)


# The output is super wide and hard to read in console output,

# but hopefully this confirms the output is (close to) what you need

res

                                                adjective_definition  \

0                       Not taut or held tightly in position; loose.   

1  (of business) characterized by a lack of work or activity; quiet.   

2                          Having or showing laziness or negligence.   

3                            (of a tide) neither ebbing nor flowing.   


                                          adjective_example  \

0                                              a slack rope   

1                                 business was rather slack   

2                               slack accounting procedures   

3  soon the water will become slack, and the tide will turn   


                                                                adjective_synonyms  \

0                            [loose, limp, not taut, not tight, hanging, flapping]   

1                                                                              NaN   

2  [lax, negligent, neglectful, remiss, careless, slapdash, slipshod, lackadais...   

3                                                                              NaN   


                                                                noun_definition  \

0  The part of a rope or line which is not held taut; the loose or unused part.   

1                                                              Casual trousers.   

2                                            A spell of inactivity or laziness.   

3                                                                           NaN   


                                                        noun_example  \

0                         I picked up the rod and wound in the slack   

1                                                                NaN   

2  he slept deeply, refreshed by a little slack in the daily routine   

3                                                                NaN   


                                                                     noun_synonyms  \

0                                                          [looseness, play, give]   

1                                                                              NaN   

2  [lull, pause, respite, spell of inactivity, interval, break, hiatus, breathi...   

3                                                                              NaN   


                                        verb_definition  \

0                Loosen (something, especially a rope).   

1  Decrease or reduce in intensity, quantity, or speed.   

2                                Work slowly or lazily.   

3                                         Slake (lime).   


                                      verb_example  \

0                                              NaN   

1                    the flow of blood slacked off   

2  she reprimanded her girls if they were slacking   

3                                              NaN   


                                                                     verb_synonyms  \

0                                                                              NaN   

1                               [reduce, lessen, slacken, slow, ease off, ease up]   

2  [idle, shirk, be inactive, be lazy, be indolent, sit back and do nothing, wa...   

3                                                                              NaN   


  adverb_definition                                          adverb_example  

0          Loosely.  their heads were hanging slack in attitudes of despair  

1               NaN                                                     NaN  

2               NaN                                                     NaN  

3               NaN                                                     NaN  


查看完整回答
反對(duì) 回復(fù) 2021-10-12
  • 3 回答
  • 0 關(guān)注
  • 333 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)