Group by multiple keys and summarize/average values of a list of dictionaries

后端 未结 7 1263
梦谈多话
梦谈多话 2020-11-30 01:03

What is the most pythonic way to group by multiple keys and summarize/average values of a list of dictionaries in Python please? Say I have a list of dictionaries as below:<

7条回答
  •  天命终不由人
    2020-11-30 01:19

    @thefourtheye If we use groupby only one key, we should check the type of key after group, if not a tuple, return a list.

    for key, grp in groupby(sorted(input_data, key = grouper), grouper):
      if not isinstance(key, tuple):
        key = [key]
    

提交回复
热议问题