Deb*_*bie 0 python text-processing dataframe pandas
我正在尝试编写一个函数来对 Pandas 数据帧的指定列(描述、事件名称)进行一些文本处理。我写了这段代码:
#removal of unreadable chars, unwanted spaces, words of at most length two from 'description' column and lowercase the 'description' column
def data_preprocessing(source):
return source.replace('[^A-Za-z]',' ')
#data['description'] = data['description'].str.replace('\W+',' ')
return source.lower()
return source.replace("\s\s+" , " ")
return source.replace('\s+[a-z]{1,2}(?!\S)',' ')
return source.replace("\s\s+" , " ")
data['description'] = data['description'].apply(lambda row: data_preprocessing(row))
data['event_name'] = data['event_name'].apply(lambda row: data_preprocessing(row))
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-94-cb5ec147833f> in <module>()
----> 1 data['description'] = data['description'].apply(lambda row: data_preprocessing(row))
2 data['event_name'] = data['event_name'].apply(lambda row: data_preprocessing(row))
3
4 #df['words']=df['words'].apply(lambda row: eliminate_space(row))
5
~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-94-cb5ec147833f> in <lambda>(row)
----> 1 data['description'] = data['description'].apply(lambda row: data_preprocessing(row))
2 data['event_name'] = data['event_name'].apply(lambda row: data_preprocessing(row))
data['description'] = data['description'].str.replace('\W+',' ')
<ipython-input-93-fdfec5f52a06> in data_preprocessing(source)
3 def data_preprocessing(source):
4
----> 5 return source.replace('[^A-Za-z]',' ')
6 #data['description'] = data['description'].str.replace('\W+',' ')
7 source = source.lower()
AttributeError: 'float' object has no attribute 'replace'
Run Code Online (Sandbox Code Playgroud)
如果我按以下方式编写代码,没有功能,它可以完美运行:
data['description'] = data['description'].str.replace('[^A-Za-z]',' ')
Run Code Online (Sandbox Code Playgroud)
有两件事要解决:
首先,当您apply将 lambda 函数应用于 pandas 系列时,lambda 函数将应用于系列的每个元素。我认为您需要以矢量化方式将您的功能应用于整个系列。
其次,您的函数有多个返回语句。结果,只有第一个语句return source.replace('[^A-Za-z]',' '),将永远运行。您需要做的是对source函数内部的变量进行预处理更改,最后在最后返回修改后的source(或中间变量)。
重写你的函数,在整个大熊猫系列操作,更换的每次出现source.用source.str.。新函数定义:
def data_preprocessing(source):
source = source.str.replace('[^A-Za-z]',' ')
#data['description'] = data['description'].str.replace('\W+',' ')
source = source.str.lower()
source = source.str.replace("\s\s+" , " ")
source = source.str.replace('\s+[a-z]{1,2}(?!\S)',' ')
source = source.str.replace("\s\s+" , " ")
return source
Run Code Online (Sandbox Code Playgroud)
然后,而不是这样:
data['description'] = data['description'].apply(lambda row: data_preprocessing(row))
data['event_name'] = data['event_name'].apply(lambda row: data_preprocessing(row))
Run Code Online (Sandbox Code Playgroud)
尝试这个:
data['description'] = data_preprocessing(data['description'])
data['event_name'] = data_preprocessing(data['event_name'])
Run Code Online (Sandbox Code Playgroud)