如果句子中有重复的单词,如何删除行

20jt8wwn  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(373)

我有一张单子

top = ['GME', 'MVIS', 'TSLA', 'AMC']

我有一个数据集

dt  ...                                               text
0       2021-03-19 20:59:49+06  ...  I only need TSLA TSLA TSLA TSLA to hit 20 eod to make up for a...
1       2021-03-19 20:59:51+06  ...                                 Oh this isn’t good
2       2021-03-19 20:59:51+06  ...  lads why is my account covered in more GME ...
3       2021-03-19 20:59:51+06  ...  I'm tempted to drop my last 800 into some TSLA...

所以我要做的是检查这个句子是否包含了超过3个单词,从列表中我想删除这一行
谢谢你的帮助

ktca8awb

ktca8awb1#

让我们编写一个函数,确定在给定的句子中,是否有“top”列表中的3个以上的单词:

def check_words(sentence,top):
    words = sentence.split()
    count = 0
    for word in words :
        if word in top :
             count+=1
    return(count>3)

然后,无论句子是否包含列表中的3个以上单词,都要创建一列true/false。让我们使用Pandas Dataframe 结构:

dataframe['Contains_3+_words'] = dataframe.apply(lambda r : check_words(r.text,top), axis=1)

然后我们只保留列表中没有包含3+个单词的句子的行:

dataframe = dataframe[dataframe['Contains_3+_words']==False]]

此外,您可以删除我们创建的列:

dataframe.drop(['Contains_3+_words'], axis=1, inplace=True)

相关问题