Python 替换我的功能的更好方法?
我附加了一个json数据链接供下载- 目前,我已经编写了以下函数,用于将每一级的子数据放入一个组合数据框中-Python 替换我的功能的更好方法?,python,json,pandas,dataframe,python-3.6,Python,Json,Pandas,Dataframe,Python 3.6,我附加了一个json数据链接供下载- 目前,我已经编写了以下函数,用于将每一级的子数据放入一个组合数据框中- def get_children(catMapping): level4 = json_normalize(catMapping['SuccessResponse']['Body'], ['children', 'children', 'children', 'children', ['childr
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
嗯,你为什么要用
literal\u eval
而不是json
包呢?哦,我明白了,数据不是真正的json。单引号而不是双引号。谢谢@HYRY。这很有帮助。我将对此进行测试,并很快分享结果。