首页 > 解决方案 > Seaborn Countplot 中的布尔掩码

问题描述

我想应用这个布尔掩码

csv["country"].value_counts()>5000

到这个函数

sns.countplot(y = csv["country"].value_counts()>5000, data = csv)

但它会引发这个错误:

"Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)".

我应该如何进行?

标签: pythonpandasseaborn

解决方案


您可以执行以下操作:

s = csv['country'].value_counts()

s[s > 5000].plot(kind='bar')

要使用 seaborn,您可以使用以下方法过滤数据:

s = csv['country'].value_counts()
s = s[s > 5000].index.tolist()

sns.countplot(x='country', data=csv.query("country in @s")) # option1
# sns.countplot(x='country', data=csv.loc[df["country"].isin(s))) # option2

推荐阅读