为了账号安全,请及时绑定邮箱和手机立即绑定

将嵌套的 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]

从那里你可以得到你想要得到的值。可以应用类似的解决方法来获取must_have.


查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信