【发布时间】:2021-11-22 18:56:59
【问题描述】:
我有一个这样的字典列表:
list1 = [{'name': 'maik','is_payed': 1, 'brand': 'HP', 'count': 1, 'items': [{'device': 'mouse', 'count': 110}]},{'name': 'milanie','is_payed': 0, 'brand': 'dell', 'count':10, 'items': [{'device': 'bales', 'count': 200}]}]
list2 = [{'name': 'maik','is_payed': 0, 'brand': 'HP', 'count': 20, 'items': [{'device': 'mouse', 'count': 1}]},{'name': 'nikola','is_payed': 1, 'brand': 'toshiba', 'count':10, 'items': [{'device': 'hard', 'count': 20}]}]
my_list= list1 + list2
count = pd.DataFrame(my_list).groupby(['name', 'is_payed'])
final_list_ = []
for commande, group in count:
print(commande)
records = group.to_dict("records")
final_list_.append({"name": commande[0],
"payed": commande[1],
"occurrence": len(group),
"items": pd.DataFrame(records).groupby('device').agg(
occurrence=('device', 'count')).reset_index().to_dict('records')})
我不知道我怎样才能得到它:
'payed' 字段是这样的 payed/total_commands
例如让maik他有两个命令,一个是付费的,另一个是不付费的,所以最终的结果会是这样的:
{'name': 'maik','payed': 1/2, 'brand': 'HP', 'count': 21, 'items': [{'device': 'mouse', 'count': 111}]}
【问题讨论】:
-
这个问题是否严格与如何维护/计算“付费”数据点有关?我之所以问,是因为我认为有很多与品牌和数量相关的事情可能很重要,但您似乎没有按足够的信息进行分组来总结它们。
-
你能告诉我更多细节吗
-
如果“Maik”拥有“东芝”品牌会怎样?那么事情是如何总结的呢?
-
我只想按“姓名”分组并获得“付费”点,因为我只是在跟踪付费/未付费命令
标签: python-3.x dictionary