【发布时间】:2018-08-09 23:11:01
【问题描述】:
您好,我是 Python 新手,我一直在尝试编写一个函数来清理存储在数据框中的文本数据。
def clean(dataset):
dataset = dataset.apply(lambda x: " ".join(x.lower() for x in x.split()))
dataset = dataset.str.replace('[^\w\s]','')
from nltk.corpus import stopwords
stop = stopwords.words('english')
dataset = dataset.apply(lambda x: " ".join(x for x in x.split() if x not in stop))
所以当我像“clean(df['cmets']”这样调用上述函数时,我希望将带有数据框的 cmets 列替换为函数中的已清理文本。 TIA。
【问题讨论】:
-
您的数据是如何存储的?使用纯 python 或类似 pandas 的东西?
标签: python pandas function dataframe series