【发布时间】:2021-02-17 12:22:03
【问题描述】:
我有以下称为句子的数据框
data = ["Home of the Jacksons"], ["Is it the real thing?"], ["What is it with you?"], [ "Tomatoes are the best"] [ "I think it's best to path ways now"]
sentences = pd.DataFrame(data, columns = ['sentence'])
还有一个称为停用词的数据框:
data = [["the"], ["it"], ["best"], [ "is"]]
stopwords = pd.DataFrame(data, columns = ['word'])
我想从句子["sentence"] 中删除所有停用词。我尝试了下面的代码,但它不起作用。我认为我的 if 语句有问题。有人可以帮忙吗?
Def remove_stopwords(input_string, stopwords_list):
stopwords_list = list(stopwords_list)
my_string_split = input_string.split(' ')
my_string = []
for word in my_string_split:
if word not in stopwords_list:
my_string.append(word)
my_string = " ".join(my_string)
return my_string
sentence['cut_string']= sentence.apply(lambda row: remove_stopwords(row['sentence'], stopwords['word']), axis=1)
当我应用该函数时,它只返回句子中的前几个字符串,但根本不删除停用词。有点卡在这里
【问题讨论】:
-
请修正代码中的语法错误。
标签: python pandas dataframe stop-words