pandas groupby to nested json

后端 未结 4 930
再見小時候
再見小時候 2020-11-27 05:47

I often use pandas groupby to generate stacked tables. But then I often want to output the resulting nested relations to json. Is there any way to extract a nested json fil

4条回答
  •  一生所求
    2020-11-27 06:29

    I'm aware this is an old question, but I came across the same issue recently. Here's my solution. I borrowed a lot of stuff from chrisb's example (Thank you!).

    This has the advantage that you can pass a lambda to get the final value from whatever enumerable you want, as well as for each group.

    from collections import defaultdict
    
    def dict_from_enumerable(enumerable, final_value, *groups):
        d = defaultdict(lambda: defaultdict(dict))
        group_count = len(groups)
        for item in enumerable:
            nested = d
            item_result = final_value(item) if callable(final_value) else item.get(final_value)
            for i, group in enumerate(groups, start=1):
                group_val = str(group(item) if callable(group) else item.get(group))
                if i == group_count:
                    nested[group_val] = item_result
                else:
                    nested = nested[group_val]
        return d
    

    In the question, you'd call this function like:

    dict_from_enumerable(grouped.itertuples(), 'amount', 'year', 'office', 'candidate')
    

    The first argument can be an array of data as well, not even requiring pandas.

提交回复
热议问题