pandas 绘制前 10 条与所有其他值的对比图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29219055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot top 10 verse all other values
提问by Brant Mullinix
I have a set of information and I want to grab the TOP 10 values verse the everything else. To elaborate I want to add all the values that are not in the TOP 10 together and add them to say a pie chart labeled as "others" along with the top 10. Currently I have the following code where X is my dataframe:
我有一组信息,我想获取 TOP 10 值与其他所有值。为了详细说明,我想将所有不在 TOP 10 中的值添加在一起,并将它们添加到标记为“others”的饼图以及前 10 名中。目前我有以下代码,其中 X 是我的数据框:
temp = X.SOME_IDENTIFIER.value_counts()
temp.head(10).plot(kind='pie')
This gets me a pie chart of just the top ten but I do not wish to discard all the other values from the dataframe. I want to add them as an eleventh variable on the chart but am not sure how to do this. Any help or advice is appreciated.
这为我提供了前十名的饼图,但我不希望丢弃数据框中的所有其他值。我想将它们添加为图表上的第十一个变量,但我不确定如何执行此操作。任何帮助或建议表示赞赏。
回答by Alexander
Assign the results to a new dataframe (temp2), and then insert a new record that sums any remaining items in the list. It also identifies the number of unique items remaining.
将结果分配给一个新的数据框 (temp2),然后插入一个新记录,该记录对列表中的所有剩余项目求和。它还标识剩余的唯一项目的数量。
temp = X.SOME_IDENTIFIER.value_counts()
temp2 = temp.head(10)
if len(temp) > 10:
temp2['remaining {0} items'.format(len(temp) - 10)] = sum(temp[10:])
temp2.plot(kind='pie')

