亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將嵌套的 json 響應規范化為具有不一致鍵的任意嵌套級別的數據幀

將嵌套的 json 響應規范化為具有不一致鍵的任意嵌套級別的數據幀

ABOUTYOU 2023-03-16 11:16:27
我正在努力將 JSON 響應轉換為我想用于各種其他操作的熊貓數據框。我已經嘗試過此處列出的方法。但問題是我無法json_normalize有效使用,因為如果我將所需的鍵作為record_path參數傳遞,則會拋出錯誤,因為只有一些字段有這個鍵,而不是全部。我不想遍歷整個 JSON 并逐個比較鍵并重新創建我自己的字典對象。我想獲取帶有uuidand nice_to_have_skills, nice_to_have_skills_path,nice_to_have_experience作為列的數據框,在這些列中,可以在 json 對象中的和鍵nice_to_have下找到這些屬性。nice_to_haveoperands我想"nice_to_have_skill" -> ["user research", "Wireframing / Prototyping"]在我的數據框中像這樣nice_to_have_skill提取列名和["user research", "Wireframing / Prototyping"]該列中的值。編輯:如果 JSON 具有任意深度,如何處理它?例如{“nice_to_have”:[{“運算符”:“AND”,“操作數”:[{“運算符”:“OR”,“操作數”:[{“類別”:“語言”,“值”:[{“ value": "Korean", "clusters": []}]}]}]}], "company_name": "Framework", "company_role": ["Manufacturing", "Supply Chain/Procurement"]}是一部分的 JSON 并且可以有任何級別的嵌套。
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

傳遞d['hits']json_normalize結果:

d = json.loads(json_text)


In [136]: %time pd.json_normalize(d['hits'])                                                                                                                                                                                                                                       

CPU times: user 2.1 ms, sys: 41 μs, total: 2.14 ms

Wall time: 2.12 ms

Out[136]: 

                                   uuid text_about                                           objectID      search_space is_searchspace                                       nice_to_have                                          must_have          some key          some_key

0  00000000-0000-0000-0000-000000000000  some_text    00000000-0000-0000-0000-000000000000-text_about               NaN            NaN                                                NaN                                                NaN               NaN               NaN

1  00000000-0000-0000-0000-000000000000        NaN  00000000-0000-0000-0000-000000000000-search_space  some json object           True                                                NaN                                                NaN               NaN               NaN

2  00000000-0000-0000-0000-000000000000        NaN  00000000-0000-0000-0000-000000000000-nice_to_have               NaN            NaN  [{'operator': 'AND', 'operands': [{'category':...                                                NaN               NaN               NaN

3  00000000-0000-0000-0000-000000000000        NaN     00000000-0000-0000-0000-000000000000-must_have               NaN            NaN                                                NaN  [{'operator': 'AND', 'operands': [{'category':...               NaN               NaN

4                                   NaN        NaN                                                NaN               NaN            NaN                                                NaN                                                NaN  some json object               NaN

5  10000000-0000-0000-0000-000000000001  some text    10000000-0000-0000-0000-000000000001-text_about               NaN            NaN                                                NaN                                                NaN               NaN               NaN

6  10000000-0000-0000-0000-000000000001        NaN  10000000-0000-0000-0000-000000000001-search_space  some json object           True                                                NaN                                                NaN               NaN               NaN

7  10000000-0000-0000-0000-000000000001        NaN  10000000-0000-0000-0000-000000000001-nice_to_have               NaN            NaN  [{'operator': 'AND', 'operands': [{'category':...                                                NaN               NaN               NaN

8  10000000-0000-0000-0000-000000000001        NaN     10000000-0000-0000-0000-000000000001-must_have               NaN            NaN                                                NaN  [{'operator': 'AND', 'operands': [{'category':...               NaN               NaN

9                                   NaN        NaN                                                NaN               NaN            NaN                                                NaN                                                NaN               NaN  some json object

在那里你可以選擇nice_to_have:


df = pd.json_normalize(d, record_path=['hits'])


In [263]: %time df['nice_to_have'].dropna().sum()                                                                                                                                                                                                                                  

CPU times: user 705 μs, sys: 11 μs, total: 716 μs

Wall time: 713 μs

Out[263]: 

[{'operator': 'AND',

  'operands': [{'category': 'Skill',

    'values': [{'value': 'MySQL ', 'clusters': []}]}]},

 {'operator': 'AND',

  'operands': [{'category': 'Skill',

    'values': [{'value': 'Frontend Programming Language ',

      'clusters': [{'key': 'Programming Language~>Frontend Programming Language',

        'name': 'Frontend Programming Language',

        'path': ['Programming Language', 'Frontend Programming Language'],

        'uuid': 'e8c5cc6c-d92b-4098-8965-41e6818fe337',

        'category': 'skill',

        'pretty_lineage': ['Programming Language']}]}]}]}]

希望這有用。


編輯:


回應您的評論:此 json 的主要問題是級別不一致,因此無法執行規范化并引發 KeyError。


獲得以下解決方法nice_to_have:


f = list(filter(lambda x: 'nice_to_have' in x, d['hits']))  


>> pd.json_normalize(f, ['nice_to_have', 'operands', 'values', 'clusters'])


                                                 key                           name                                               path                                  uuid category          pretty_lineage

0  Programming Language~>Frontend Programming Lan...  Frontend Programming Language  [Programming Language, Frontend Programming La...  e8c5cc6c-d92b-4098-8965-41e6818fe337    skill  [Programming Language]

從那里你可以得到你想要得到的值??梢詰妙愃频慕鉀Q方法來獲取must_have.


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號