亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

字典中小于某個值的總和值

字典中小于某個值的總和值

慕村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條經驗 獲得超7個贊

基于@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 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號