【问题标题】:How to select multiple specific values from a column in dataframe for plotting如何从数据框中的列中选择多个特定值进行绘图
【发布时间】:2020-10-30 21:02:56
【问题描述】:

我有一个 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']) 

这是一个失败。

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

【问题讨论】:

  • 尝试以下操作:sns.countplot(x=df.Label, data=df[((df.Label != 'I-Protein') & (df.Label != 'I-DNA'))].

标签: python pandas plot label seaborn


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-11-01
    • 1970-01-01
    • 2020-10-13
    • 2020-06-23
    • 2021-03-19
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多