【问题标题】:Consolidating non-duplicate rows of a dataframe合并数据框的非重复行
【发布时间】:2021-12-29 13:49:02
【问题描述】:

我正在开发一种自动化解决方案,以在 Python 中训练二进制相关多标签分类模型。我正在使用skmultilearn,其关键元素是 TFIDF 矢量化器和BinaryRelevance(MultinomialNB()) 函数。

我遇到了准确性问题,需要提高训练数据的质量。

这是非常耗费人力的(在 Excel 中阅读或手动过滤数百篇新闻文章),因此我正在寻找使其自动化的方法。我的数据来自一个大学数据库,我在其中搜索与我正在学习的内容相关的文章。我的最终目标是为一篇文章可以有零个、一个或多个标签的所有文章分配六个标签。我目前快速生成训练数据的想法是使用每个标签的标准搜索大学数据库,然后对其进行标记以生成如下所示的内容:

ID Title Full Text Label 1 Label 2 Search Criteria
0 Article 1 blahblah 1 0 Search terms associated with label 1
1 Article 2 blah 1 0 Search terms associated with label 1
2 Article 2 blah 0 1 Search terms associated with label 2
3 Article 4 balala 0 1 Search terms associated with label 2
4 Article 5 baaa 0 1 Search terms associated with label 2

这样做会多次返回具有多个标签的同一篇文章。上面显示了满足标签 1 和 2 的搜索条件的文章 2。我现在需要将这些实例合并到此:

ID Title Full Text Label 1 Label 2
1 Article 2 blah 1 1

而不是这个:

ID Title Full Text Label 1 Label 2 Search Criteria
1 Article 2 blah 1 0 label 1
2 Article 2 blah 0 1 label 2

我对 Python 数据处理非常陌生。我第一次探索 Python 是为了探索它的 NLP 包。关于如何解决这个问题的任何想法?是否有一些我可以使用的 pandas 数据框功能?

【问题讨论】:

    标签: python pandas dataframe data-cleaning


    【解决方案1】:

    试试这个:

    df.groupby('Title').agg('max').reset_index().drop('Search Criteria', axis=1)
    

    之前:

       ID      Title Full Text  Label 1  Label 2                       Search Criteria
    0   0  Article 1  blahblah        1        0  Search terms associated with label 1
    1   1  Article 2      blah        1        0  Search terms associated with label 1
    2   2  Article 2      blah        0        1  Search terms associated with label 2
    3   3  Article 4    balala        0        1  Search terms associated with label 2
    4   4  Article 5      baaa        0        1  Search terms associated with label 2
    

    之后:

           Title  ID Full Text  Label 1  Label 2
    0  Article 1   0  blahblah        1        0
    1  Article 2   2      blah        1        1 <----- Notice that there is only one "Article 2" row, and "Label 1" and "Label 2" are both 1
    2  Article 4   3    balala        0        1
    3  Article 5   4      baaa        0        1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2021-11-11
      • 2018-09-17
      相关资源
      最近更新 更多