如何聚合按列表中的特定属性分组的特定属性值 [英] How to aggregate particular property value that is group by a particular property in a list

查看:81
本文介绍了如何聚合按列表中的特定属性分组的特定属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表客户购买商品的列表:

I have a list which represent the item purchase of a customer:

purchases = [
   { 
       'id': 1, 'product': 'Item 1', 'price': 12.4, 'qty' : 4
   }, 
   { 
       'id': 1, 'product': 'Item 1', 'price': 12.4, 'qty' : 8
   },
   { 
       'id': 2, 'product': 'Item 2', 'price': 7.5, 'qty': 10
   }, 
   { 
       'id': 3, 'product': 'Item 3', 'price': 18, 'qty': 7
   }
]

现在,我希望输出返回具有聚集数量的独特product.

Now i want the output that returns the distinct product with an aggregated qty.

result = [
   { 
       'id': 1, 'product': 'Item 1', 'price': 12.4, 'qty' : 12 # 8 + 4
   }, 
   { 
       'id': 2, 'product': 'Item 2', 'price': 7.5, 'qty': 10
   }, 
   { 
       'id': 3, 'product': 'Item 3', 'price': 18, 'qty': 7
   }
]

这里的答案对我来说毫无意义 如何对字典元素求和

And the answers here never makes sense to me How to sum dict elements

推荐答案

熊猫中,它很简单- groupby aggregate ,最后一个 to_dict :

In pandas it is simple - groupby with aggregate, last to_dict:

import pandas as pd

df = pd.DataFrame(purchases)
print (df)
   id  price product  qty
0   1   12.4  Item 1    4
1   1   12.4  Item 1    8
2   2    7.5  Item 2   10
3   3   18.0  Item 3    7

print (df.groupby('product', as_index=False)
         .agg({'id':'first','price':'first','qty':'sum'})
         .to_dict(orient='records'))

[{'qty': 12, 'product': 'Item 1', 'price': 12.4, 'id': 1}, 
 {'qty': 10, 'product': 'Item 2', 'price': 7.5, 'id': 2}, 
 {'qty': 7, 'product': 'Item 3', 'price': 18.0, 'id': 3}]

如果可以按3个元素分组:

If is possible groupby by 3 elements:

print (df.groupby(['id','product', 'price'], as_index=False)['qty'].sum()
         .to_dict(orient='records'))
[{'qty': 12, 'product': 'Item 1', 'id': 1, 'price': 12.4}, 
 {'qty': 10, 'product': 'Item 2', 'id': 2, 'price': 7.5}, 
 {'qty': 7, 'product': 'Item 3', 'id': 3, 'price': 18.0}]


from itertools import groupby
from operator import itemgetter

grouper = itemgetter("id", "product", "price")
result = []
for key, grp in groupby(sorted(purchases, key = grouper), grouper):
    temp_dict = dict(zip(["id", "product", "price"], key))
    temp_dict["qty"] = sum(item["qty"] for item in grp)
    result.append(temp_dict)

print(result)
[{'qty': 12, 'product': 'Item 1', 'id': 1, 'price': 12.4}, 
 {'qty': 10, 'product': 'Item 2', 'id': 2, 'price': 7.5}, 
 {'qty': 7, 'product': 'Item 3', 'id': 3, 'price': 18}]

通过评论

purchases = [
   { 
       'id': 1, 'product': { 'id': 1, 'name': 'item 1' }, 'price': 12.4, 'qty' : 4
   }, 
   { 
       'id': 1, 'product': { 'id': 1, 'name': 'item 2' }, 'price': 12.4, 'qty' : 8
   },
   { 
       'id': 2, 'product':{ 'id': 2, 'name': 'item 3' }, 'price': 7.5, 'qty': 10
   }, 
   { 
       'id': 3, 'product': { 'id': 3, 'name': 'item 4' }, 'price': 18, 'qty': 7
   }
]

from pandas.io.json import json_normalize    
df = json_normalize(purchases)
print (df)
   id  price  product.id product.name  qty
0   1   12.4           1       item 1    4
1   1   12.4           1       item 2    8
2   2    7.5           2       item 3   10
3   3   18.0           3       item 4    7

print (df.groupby(['id','product.id', 'price'], as_index=False)['qty'].sum()
         .to_dict(orient='records'))

[{'qty': 12.0, 'price': 12.4, 'id': 1.0, 'product.id': 1.0}, 
 {'qty': 10.0, 'price': 7.5, 'id': 2.0, 'product.id': 2.0}, 
 {'qty': 7.0, 'price': 18.0, 'id': 3.0, 'product.id': 3.0}]

这篇关于如何聚合按列表中的特定属性分组的特定属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆