【问题标题】:Boolean mask within Seaborn CountplotSeaborn Countplot 中的布尔掩码
【发布时间】:2020-01-19 01:31:58
【问题描述】:

我想应用这个布尔掩码

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)".

我应该如何进行?

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    你可以这样做:

    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
    

    【讨论】:

    • 这可行,但我想使用 sns.countplot 函数。如果我输入 sns.countplot(y = s[s>1000], data=csv) 我没有得到我想要的
    • 非常感谢!一旦我们将s 转换为列表,有没有办法将data = 设置为s 而不必使用csv.query?就像sns.countplot(y='country', data=s)
    • 您需要过滤数据,您可以尝试使用.loc来过滤数据的选项2。
    • 我们也可以用这种方式重写选项 2 sns.countplot(y='country', data=csv[csv["country"].isin(s)]),因为我们不需要 .loc 来访问列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2013-05-17
    • 2017-07-22
    • 2023-01-23
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多