gpt4 book ai didi

python - 替换我的功能的更好方法?

转载 作者:行者123 更新时间:2023-12-01 01:53:12 26 4
gpt4 key购买 nike

我附上了一个json数据链接供下载-

json data

目前我已经编写了以下函数,用于将每个级别的子数据放入组合数据框中 -

def get_children(catMapping):
level4 = json_normalize(catMapping['SuccessResponse']['Body'],
['children', 'children', 'children', 'children', ['children']])
level3 = json_normalize(catMapping['SuccessResponse']['Body'],
['children', 'children', 'children', ['children']])
['children', 'children', ['children']])
level1 = json_normalize(catMapping['SuccessResponse']['Body'],
['children', ['children']])
level0 = json_normalize(catMapping['SuccessResponse']['Body'],
['children'])

combined = pd.concat([level0, level1, level2, level3,level4])
combined = combined.reset_index(drop=True)

return combined

看起来这不是推荐的方法,但我无法编写一个可以遍历每个级别的函数。

你能帮我提供更好的功能吗?

最佳答案

这是一个递归迭代所有项目的函数:

import pandas as pd
import ast

with open(r"data.json", "r") as f:
data = ast.literal_eval(f.read())

def nest_iter(items):
for item in items:
children_ids = [o["categoryId"] for o in item["children"]]
ret_item = item.copy()
ret_item["children"] = children_ids
yield ret_item
yield from nest_iter(item["children"])

df = pd.DataFrame(nest_iter(data['SuccessResponse']['Body']))

结果:

      categoryId                        children   leaf         name    var
....
4970 10001244 [] True Business False
4971 10001245 [] True Casual False
4972 10001246 [] True Fashion False
4973 10001247 [] True Sports False
4974 7756 [7761, 7758, 7757, 7759, 7760] False Women False
4975 7761 [] True Accessories False
4976 7758 [] True Business False
4977 7757 [] True Casual False
4978 7759 [] True Fashion False
4979 7760 [] True Sports False

关于python - 替换我的功能的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com