为了账号安全,请及时绑定邮箱和手机立即绑定

字典中小于某个值的总和值

字典中小于某个值的总和值

慕村225694 2021-09-11 15:13:37
我有以下的解释和我试图让他们从一个饼图,但我想只包括前5名(他们被这里的值已经排序),然后在总结他人一起Other类别即更换Publishing,Fashion,Food等只一个Other将它们加在一起。坚持如何做到这一点,所以将不胜感激任何帮助!{'Games': 715067930.8599964, 'Design': 705237125.089998, 'Technology': 648570433.7599969, 'Film & Video': 379559714.56000066, 'Music': 191227757.8699999, 'Publishing': 130763828.65999977, 'Fashion': 125678824.47999984, 'Food': 122781563.58000016, 'Art': 89078801.8599998, 'Comics': 70600202.99999984, 'Theater': 42662109.69999992, 'Photography': 37709926.38000007, 'Crafts': 13953818.35000002, 'Dance': 12908120.519999994, 'Journalism': 12197353.370000007}目前我的饼图使用此代码真的人满为患groupbycategorypledge = df.groupby('main_category')['usd_pledged_real'].sum().sort_values(ascending=False)plt.figure(figsize=(20, 10))pie = groupbycategorypledge.plot(kind='pie', startangle=90, radius=0.7, title='Amount Pledged by Main category',autopct='%1.1f%%',labeldistance=1.2)plt.legend(loc=(1.05,0.75))plt.ylabel('')所以我有dict = groupbycategorypledge.sort_values(ascending=False).to_dict()
查看完整描述

2 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

你可以在使用 Pandas之前操作你的字典:


from operator import itemgetter


# sort by value descending

items_sorted = sorted(d.items(), key=itemgetter(1), reverse=True)


# calculate sum of others

others = ('Other', sum(map(itemgetter(1), items_sorted[5:])))


# construct dictionary

d = dict([*items_sorted[:5], others])


print(d)


{'Games': 715067930.8599964,

 'Design': 705237125.089998,

 'Technology': 648570433.7599969,

 'Film & Video': 379559714.56000066,

 'Music': 191227757.8699999,

 'Other': 658334549.8999995}


查看完整回答
反对 回复 2021-09-11
?
LEATH

TA贡献1936条经验 获得超6个赞

基于@jpp 的想法,但使用堆:


import heapq


d = {'Games': 715067930.8599964,

     'Design': 705237125.089998,

     'Technology': 648570433.7599969,

     'Film & Video': 379559714.56000066,

     'Music': 191227757.8699999,

     'Publishing': 130763828.65999977,

     'Fashion': 125678824.47999984,

     'Food': 122781563.58000016,

     'Art': 89078801.8599998,

     'Comics': 70600202.99999984,

     'Theater': 42662109.69999992,

     'Photography': 37709926.38000007,

     'Crafts': 13953818.35000002,

     'Dance': 12908120.519999994,

     'Journalism': 12197353.370000007}


top_5 = set(heapq.nlargest(5, d, key=d.get))


groups = {}

for category, pledge in d.items():

    new_category = category if category in top_5 else 'Other'

    groups.setdefault(new_category, []).append(pledge)


result = {k: sum(v) for k, v in groups.items()}

print(result)

输出


{'Technology': 648570433.7599969, 'Design': 705237125.089998, 'Other': 658334549.8999994, 'Games': 715067930.8599964, 'Film & Video': 379559714.56000066, 'Music': 191227757.8699999}

或者,如果您喜欢 numpy:


import numpy as np


d = {'Games': 715067930.8599964,

     'Design': 705237125.089998,

     'Technology': 648570433.7599969,

     'Film & Video': 379559714.56000066,

     'Music': 191227757.8699999,

     'Publishing': 130763828.65999977,

     'Fashion': 125678824.47999984,

     'Food': 122781563.58000016,

     'Art': 89078801.8599998,

     'Comics': 70600202.99999984,

     'Theater': 42662109.69999992,

     'Photography': 37709926.38000007,

     'Crafts': 13953818.35000002,

     'Dance': 12908120.519999994,

     'Journalism': 12197353.370000007}


categories, pledge_values = map(np.array, zip(*d.items()))

partition = np.argpartition(pledge_values, -5)

top_5 = set(categories[partition][-5:])


groups = {}

for category, pledge in d.items():

    new_category = category if category in top_5 else 'Other'

    groups.setdefault(new_category, []).append(pledge)


result = {k: sum(v) for k, v in groups.items()}

print(result)

输出


{'Technology': 648570433.7599969, 'Design': 705237125.089998, 'Other': 658334549.8999995, 'Music': 191227757.8699999, 'Games': 715067930.8599964, 'Film & Video': 379559714.56000066}

第二个提案(使用 numpy)的复杂度是O(n),其中n是 的键值对的数量d。


查看完整回答
反对 回复 2021-09-11
  • 2 回答
  • 0 关注
  • 168 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信