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

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

通過(guò)迭代嵌套字典中的第 n 層值來(lái)創(chuàng)建數(shù)據(jù)幀

通過(guò)迭代嵌套字典中的第 n 層值來(lái)創(chuàng)建數(shù)據(jù)幀

FFIVE 2023-07-18 13:57:03
我從網(wǎng)上人類患病icd-11分類下載了一個(gè)json文件,該數(shù)據(jù)最多有8層嵌套,例如:? "name":"br08403",? ? "children":[? ? {? ? ? ? "name":"01 Certain infectious or parasitic diseases",? ? ? ? "children":[? ? ? ? {? ? ? ? ? ? "name":"Gastroenteritis or colitis of infectious origin",? ? ? ? ? ? "children":[? ? ? ? ? ? {? ? ? ? ? ? ? ? "name":"Bacterial intestinal infections",? ? ? ? ? ? ? ? "children":[? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? "name":"1A00? Cholera",? ? ? ? ? ? ? ? ? ? "children":[? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? "name":"H00110? Cholera"? ? ? ? ? ? ? ? ? ? }我嘗試使用以下代碼:def flatten_json(nested_json):? ? """? ? ? ? Flatten json object with nested keys into a single level.? ? ? ? Args:? ? ? ? ? ? nested_json: A nested json object.? ? ? ? Returns:? ? ? ? ? ? The flattened json object if successful, None otherwise.? ? """? ? out = {}? ? def flatten(x, name=''):? ? ? ? if type(x) is dict:? ? ? ? ? ? for a in x:? ? ? ? ? ? ? ? flatten(x[a], name + a + '_')? ? ? ? elif type(x) is list:? ? ? ? ? ? i = 0? ? ? ? ? ? for a in x:? ? ? ? ? ? ? ? flatten(a, name + str(i) + '_')? ? ? ? ? ? ? ? i += 1? ? ? ? else:? ? ? ? ? ? out[name[:-1]] = x? ? flatten(nested_json)? ? return outdf2 = pd.Series(flatten_json(dictionary)).to_frame()我得到的輸出是:name? ? br08403children_0_name 01 Certain infectious or parasitic diseaseschildren_0_children_0_name? Gastroenteritis or colitis of infectious originchildren_0_children_0_children_0_name? ?Bacterial intestinal infectionschildren_0_children_0_children_0_children_0_name? ? 1A00 Cholera... ...children_21_children_17_children_10_name? ? NF0A Certain early complications of trauma, n...children_21_children_17_children_11_name? ? NF0Y Other specified effects of external causeschildren_21_children_17_children_12_name? ? NF0Z Unspecified effects of external causeschildren_21_children_18_name? ? NF2Y Other specified injury, poisoning or cer...children_21_children_19_name? ? NF2Z Unspecified injury, poisoning or certain..但所需的輸出是一個(gè)具有 8 列的數(shù)據(jù)框,它可以容納嵌套名稱鍵的最后深度,例如:
查看完整描述

1 回答

?
肥皂起泡泡

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

一種簡(jiǎn)單的pandas迭代方法。


res = requests.get("https://www.genome.jp/kegg-bin/download_htext?htext=br08403.keg&format=json&filedir=")

js = res.json()


df = pd.json_normalize(js)

for i in range(20):

    df = pd.json_normalize(df.explode("children").to_dict(orient="records"))

    if "children" in df.columns: df.drop(columns="children", inplace=True)

    df = df.rename(columns={"children.name":f"level{i}","children.children":"children"})

    if df[f"level{i}"].isna().all() or "children" not in df.columns: break


查看完整回答
反對(duì) 回復(fù) 2023-07-18
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報(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)