我有下面的字典,并试图从他们做一个饼图,但我只想包括前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()
您可以在使用熊猫之前操作词典:
基于@jpp的思想,但使用heap:
输出
或者如果你喜欢numpy:
输出
第二个建议(使用numpy)的复杂性是O(n),其中
n
是d
的键、值对的数目相关问题 更多 >
编程相关推荐