Seaborn Countplot:仅显示 n 个最常见的类别 [英] Seaborn Countplot : Display only n most frequent categories

查看:44
本文介绍了Seaborn Countplot:仅显示 n 个最常见的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 数组,列出所有出现的字符串标签.我们称之为labels_array.使用 seaborn 作为 sns 我想显示这个数组的计数图:

I have a python array listing all occurences of string labels. Let's call it labels_array. Using seaborn as sns I d like to show a countplot of this array :

sns.countplot(labels_array)这有效,但由于它们在我的数组中不同的标签太多,输出看起来不太好.

sns.countplot(labels_array) This works, but as they are too many different labels in my array, the outpout doesnt look good.

有没有办法只显示 n 个最常用的标签.

Is there a way to display only the n most frequent labels.

推荐答案

您可以使用 pd.value_counts() 对出现的事件进行排序.

You can use pd.value_counts() to get your occurrences sorted.

要获得前 N 个出现,您可以简单地编写 pd.value_counts(labels_array).iloc[:N].index(标签索引)

And to get the first N occurrences you can simply write pd.value_counts(labels_array).iloc[:N].index (index for labels)

你可以把它应用在 countplot 上,它应该是这样的:

you can apply it on countplot and it should look like this:

sns.countplot(labels_array, order=pd.value_counts(labels_array).iloc[:N].index)

这篇关于Seaborn Countplot:仅显示 n 个最常见的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆