我想将定义的函数"tokenization“应用于数据集"reviews_english”的列"Review Gast“的所有行。我该怎么做呢?目前,我只能将它应用于一行。谢谢!:)
def tokenization(text):
# Normalize
text = normalize(text)
# Remove Punctuation
text = remove_punctuation(text)
# Tokenize
tokens = text.split()
# Remove Stopwords
tokens = remove_stopwords(tokens)
# Apply Bag-of-Words (set of tokens)
bow = set(tokens)
return bow
clean_reviews_english =tokenization(reviews_english["Review Gast"][0])
print(clean_reviews_english)发布于 2021-06-04 17:31:51
使用列表理解
clean_reviews_english = tokenization(review for review in reviews_english["Review Gast"])或map
clean_reviews_english = map(tokenization, reviews_english["Review Gast"])发布于 2021-06-04 17:34:22
假设您使用的是pandas数据帧,如果想要将函数应用于列,请使用df["col"].apply(func)
在此示例中,要将结果添加为新列,请使用:
reviews_english["tokenized"] = reviews_english["Review Gast"].astype(str).apply(tokenization)如果你没有使用熊猫数据帧,那就使用科拉伦的答案。
https://stackoverflow.com/questions/67834847
复制相似问题