python - Python在字典中计算总值和百分比
问题描述
在这一点上我肯定做错了,我的大脑正在融化。我有这个数据
queryset = [
{'source_id': '1', 'gender_id': 'female', 'total': 12928604, 'percentage': {'neutral': [8284384, 64.08], 'positive': [3146438, 24.34], 'negative': [1497782, 11.59]}},
{'source_id': '1', 'gender_id': 'male', 'total': 15238856, 'percentage': {'neutral': [10042152, 65.9], 'positive': [2476421, 16.25], 'negative': [2720283, 17.85]}},
{'source_id': '1', 'gender_id': 'null', 'total': 6, 'percentage': {'neutral': [5, 83.33], 'positive': [1, 16.67], 'negative': [0, 0.0]}},
{'source_id': '2', 'gender_id': 'female', 'total': 23546499, 'percentage': {'neutral': [15140308, 64.3], 'positive': [5372964, 22.82], 'negative': [3033227, 12.88]}},
{'source_id': '2', 'gender_id': 'male', 'total': 15349754, 'percentage': {'neutral': [10137025, 66.04], 'positive': [2413350, 15.72], 'negative': [2799379, 18.24]}},
{'source_id': '2', 'gender_id': 'null', 'total': 3422, 'percentage': {'neutral': [2464, 72.0], 'positive': [437, 12.77], 'negative': [521, 15.23]}}
{'source_id': '3', 'gender_id': 'female', 'total': 29417761, 'percentage': {'neutral': [18944384, 64.4], 'positive': [7181996, 24.41], 'negative': [3291381, 11.19]}},
{'source_id': '3', 'gender_id': 'male', 'total': 27200788, 'percentage': {'neutral': [17827887, 65.54], 'positive': [4179990, 15.37], 'negative': [5192911, 19.09]}},
{'source_id': '3', 'gender_id': 'null', 'total': 32909, 'percentage': {'neutral': [22682, 68.92], 'positive': [4005, 12.17], 'negative': [6222, 18.91]}}
]
我想要的输出是
[ {'source_id:1', 'total': 28167466(sum of 'male, female, null' total
values for source id=1) , percentage: {'neutral':[18326541,
65.06(getting the % out of neutral value from total)], 'positive':
[5622859, 19.96], 'negative':[4218065,14.97], {and do the same for all sources}]
我做什么但不起作用,我有 3if 语句适用于所有 3 个 ID
for i in queryset:
if i['source_id'] == '1':
output['percentage'] = {
'neutral': [sum(i['percentage']['neutral'][0] for i in queryset if i['source_id'] == '1'),
round(output['negative'] / output['2_total'] * 100, 2)],
'positive': [sum(i['percentage']['positive'][0] for i in queryset if i['source_id'] == '2'),
round(output['positive'] / output['2_total'] * 100, 2)],
'negative': [sum(i['percentage']['negative'][0] for i in queryset if i['source_id'] == '2'),
round(output['negative'] / output['2_total'] * 100, 2)]}
解决方案
您可以使用collections.Counter
总计:
from collections import Counter
counters = {}
for row in queryset:
# gender_id not needed
del row['gender_id']
# Pull the subtotals from 'percentage'
# into the parent dictionary, keeping only
# the subtotals in first list item,
# not the percentages
percentages = row.pop('percentage')
for k, v in percentages.items():
percentages[k] = v[0]
row.update(percentages)
# Use 'source_id' as key for the
# counters dictionary
index = row.pop('source_id')
if index not in counters:
counters[index] = Counter(row)
else:
counters[index].update(row)
这为您提供以下信息:
{'1': Counter({'total': 28167466,
'neutral': 18326541,
'positive': 5622860,
'negative': 4218065}),
'2': Counter({'total': 38899675,
'neutral': 25279797,
'positive': 7786751,
'negative': 5833127}),
'3': Counter({'total': 56651458,
'neutral': 36794953,
'positive': 11365991,
'negative': 8490514})}
由此,您可以轻松计算百分比并将其转换为所需的格式。
推荐阅读
- python - 将 ImageField 中的图像转换为 numpy 数组
- go - 如何使用互斥锁有效地读写地图?
- javascript - 您如何在具有 chrome 扩展程序的多个网页上运行 javascript 代码(例如,如果您要自动执行 Gmail 登录 + 邮件)?
- javascript - 带有 socket.io 的 app.post 导致无法 GET
- c# - 如何在 C# 中获取 AVX512?
- flutter - 如何在macos上完全卸载flutter并以正确的方式重新安装?
- python - 使用 df.loc 为数据框单元格分配新值
- flutter - 有没有办法在不删除 Positioned.fill 小部件的情况下使这段代码工作
- node.js - NodeJs (A) 通过 SSH 连接到远程服务器 (B) 将 API 请求发送到另一台服务器 (C)
- react-native - 带有本机反应的套接字文件结构