我有一个数据框,其中有27949行和7列,而前几行如下所示
 https://i.stack.imgur.com/1Pipf.png

任务:
在数据框中,我有一个“标题”列,其中包含许多我要删除的重复标题(重复标题:除1个或2个单词外,几乎所有标题都是相同的)。
伪代码:
我想检查所有其他行的第一行,如果其中任何一个是重复的,我想将其删除。然后我想检查所有其他行的第二行&如果其中任何一个是重复的我想删除它-与所有行类似,即i =第一行到最后一行j = i + 1到最后一行
我的代码:

for i in range(0,27950):
    for j in range(1,27950):
        a = data_sorted['title'].iloc[i].split()
        b = data_sorted['title'].iloc[j].split()
        if len(a)-len(b)<=2:
            data_sorted.drop(b)
            j=j
        else:
            j+=1
    i+=1


错误:
IndexError:单个位置索引器超出范围

谁能帮我解决我的代码。
提前致谢。

最佳答案

我建议采用以下方法:

建立标题的差异矩阵,其中i,j元素代表第i个标题和第j个标题之间的单词差异。

像这样:

    import numpy as np
    from itertools import product

    l = list(data_sorted['title'])

    def diff_words(text_1, text_2):
        # return the number of different words between two texts
        words_1 = text_1.split()
        words_2 = text_2.split()
        diff = max(len(words_1),len(words_2))-len(np.intersect1d(words_1, words_2))
        return diff


    differences = [diff_words(i,j) for i,j in product(l,l)]
    # differences: a flat matrix integers where the i,j element is the word difference between titles i and j

10-08 07:06
查看更多