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

后端 未结 7 1255
梦谈多话
梦谈多话 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:35

    Using the numpy EP you can find here, you could write:

    inputs = dict( (k, [i[k] for i in input ]) for k in input[0].keys())
    print group_by((inputs['dept'], inputs['sku'])).mean(inputs['qty'])
    

    However, you may want to consider using the pandas package if you are doing a lot of relational operations of this kind.

提交回复
热议问题