我有一个数字数组,我想从Pandas中的字符串中删除这些单词。例如:如果数组中有一个单词'the‘,列'The cat’中有一个字符串。所以它应该变成“猫”。我不想删除整个字符串,只想删除那个词。
# This will iterate that numpy array
def iterate():
for x in range(0, 52):
for y in range(0, 8):
return (np_array[x,y])
# The code below drops that row/record
filtered = df[~df.content.str.contains(iterate())]的帮助将受到高度赞赏。
样本数据: numpy数组= a,约,然后,之后,in,on,as
一个样本单元格:df‘’content‘=一定要收看唐纳德·特朗普和大卫·莱特曼的深夜秀,因为他今晚将出现十大榜单!
样本输出:请务必收看唐纳德·特朗普与大卫·莱特曼的深夜秀,他今晚将推出十大榜单!
发布于 2020-03-30 11:28:31
如果您能够设法从Numpy数组中删除一个完整的停止词列表,那么您可以构建一个regexp来匹配您想要删除的所有停止词,然后使用df.replace。
stopwords = [
"a", "about", "and", "across", "after",
"afterwards", "in", "on", "as",
]
# Compile a regular expression that will match all the words in one sweep
stopword_re = re.compile("|".join(r"\b%s\b" % re.escape(word) for word in stopwords))
# Replace and reassign into the column
df["content"].replace(stopword_re, "", inplace=True)如果应用程序需要,还可以添加.replace(re.compile(r"\s+"), " ")将结果的多个空格折叠到一个空格中。
https://stackoverflow.com/questions/60929139
复制相似问题