更换我的功能更好的方法?

时间:2018-05-25 06:20:49

标签: python json pandas dataframe python-3.6

我附上了一个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

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

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

1 个答案:

答案 0 :(得分:2)

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

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