如何解决如何使用 Python 删除 csv 文件中的标点符号和停用词实例?
我有一个 csv 文件,我想在“文本”列的值为标点符号或某些停用词(例如 is 或 to)时删除记录或行。
type | text
type1,this is the text.
type1,Hi,my name is John.
type1,Here wait a second:
type2,Come to my house
type2,here I go.
我想只为文本列清除所有标点和停用词的数据,以便输出如下所示:
type | text
type1,this text
type1,Hi my name John
type1,Here wait second
type2,Come my house
type2,here go
请注意第一个表中的所有标点符号和停用词(to、I、is、a 等)如何不再位于文本列中。这似乎是一个简单的解决方法,但我似乎无法使其正常工作。
这是我的脚本:
import pandas as pd
in_file = r"data.csv"
stopwords = ['is','to','I','the','a']
df = pd.read_csv(in_file,encoding='latin-1' )
df=pd.DataFrame()
# Remove all the punctuation and stopwords in the text column only
for x in stopwords:
df['TEXT'] = df['TEXT'].re.sub(r'[^\w\s]','',word)
df['TEXT'] = df['TEXT'].re.sub(stopwords,word)
# Output to csv file
df.to_csv(r'outputfile.csv')
任何想法或建议都适用,因为我是 Python 新手。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。