Pandas to D3. Serializing dataframes to JSON

前端 未结 2 1841
时光说笑
时光说笑 2021-02-04 16:46

I have a DataFrame with the following columns and no duplicates:

[\'region\', \'type\', \'name\', \'value\']

that can be seen as a hierarchy as

2条回答
  •  无人共我
    2021-02-04 17:14

    Something along these lines might get you there.

    from collections import defaultdict
    
    tree = lambda: defaultdict(tree)  # a recursive defaultdict
    d = tree()
    for _, (region, type, name, value) in df.iterrows():
        d['children'][region]['name'] = region
        ...
    
    json.dumps(d)
    

    A vectorized solution would be better, and maybe something that takes advantage of the speed of groupby, but I can't think of such a solution.

    Also take a look at df.groupby(...).groups, which return a dict.

    See also this answer.

提交回复
热议问题