用Python清理文本数据

时间:2018-03-19 17:22:47

标签: python text nlp data-cleaning

我想在删除所有数字(例如189,98001),特殊字符(',_,“,(,))之后为文本数据创建一个新列(该列的每一行是一个描述),带有数字或特殊字符的字母(e21x16,e267,e4,e88889,entry778,id2,n27th,pv3,)。

所以我写下了这个函数。但是,返回的结果仍包含数字和特殊字符。基本上,我的目标是只保留英文单词和缩写。有谁知道为什么我的功能不起作用。

def standardize_text(df, text_field):
  df[text_field] = df[text_field].str.lower()
  df[text_field] = df[text_field].str.replace(r'(', '') 
  df[text_field] = df[text_field].str.replace(r')', '')
  df[text_field] = df[text_field].str.replace(r',', '')
  df[text_field] = df[text_field].str.replace(r'_', '')
  df[text_field] = df[text_field].str.replace(r"'", "")
  df[text_field] = df[text_field].str.replace(r"^[a-z]+\[0-9]+$", "")
  df[text_field] = df[text_field].str.replace(r"^[0-9]{1,2,3,4,5}$", "")
  return df

2 个答案:

答案 0 :(得分:1)

使用一个名为“ textcleaner”的库。请参见repositorylink。 这个article可能会对您有所帮助。

startDestination

!pip install textcleaner
import textcleaner as tc 

现在只需致电from textcleaner import * 它将返回带有所有基本预处理的单词列表。

答案 1 :(得分:-1)

您必须将inplace函数的replace参数设置为true,或将返回的df分配给df变量