如何 select 数据框中一列的多个特定值进行绘图

How to select multiple specific values from a column in dataframe for plotting

我有一个 DataFrame,其中包含一个名为“Label”的分类数据列。 这是 df.Label.value_counts() 代码的输出。

    O              382963
    B-protein       30269
    I-protein       24848
    I-DNA           15774
    B-DNA            9533
    I-cell_type      8748
    I-cell_line      7387
    B-cell_type      6718
    B-cell_line      3830
    I-RNA            1530
    B-RNA             951

    

我想使用 seaborn 库中的 countplot() 来绘制这些值,但我只想绘制 B-label 值的计数,而不是 Label 列中的所有值。

我试过了

   sns.countplot(x=df.Label, data=df[-(df.Label == 'I-Protein')]) 

它只适用于 I-Protein 标签。然后我尝试减去其他特定值,但没有成功。 例如我试过这个:

   sns.countplot(x=df.Label, data=df[-(df.Label == 'I-Protein' or 'I-DNA']) 

失败了。

有人可以帮助我吗?谢谢。

您可以制作一个仅用于绘图的 DataFrame。这样您就可以在将数据发送到 Seaborn 之前对其进行测试和调试:

df_plot = df[df['Label'].str.startswith('B-')]
sns.countplot(x='Label', data=df_plot)