大家好
我有一个数据框,其中有2510765行,包含具有相对分数的应用程序审查,并且具有以下结构:
root
|-- content: string (nullable = true)
|-- score: string (nullable = true)
我编写了这两个函数,用于从文本中删除标点符号和表情符号:
import string
def remove_punct(text):
return text.translate(str.maketrans('', '', string.punctuation))
及
import re
def removeEmoji(text):
regrex_pattern = re.compile(pattern = "["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags = re.UNICODE)
return regrex_pattern.sub(r'',text)
我使用udf
函数创建一个spark函数,从我定义的用于删除标点符号和表情的函数开始:
from pyspark.sql.functions import udf
punct_remove = udf(lambda s: remove_punct(s))
removeEmoji = udf(lambda s: removeEmoji(s))
但我得到了以下错误:
TypeError Traceback (most recent call last)
<ipython-input-29-e5d42d609b59> in <module>()
----> 1 new_df = new_df.withColumn("content", remove_punct(df_merge["content"]))
2 new_df.show(5)
<ipython-input-21-dee888ef5b90> in remove_punct(text)
2
3 def remove_punct(text):
----> 4 return text.translate(str.maketrans('', '', string.punctuation))
5
6
TypeError: 'Column' object is not callable
怎么解决呢??有没有其他方法可以让用户编写的函数在数据帧上运行
谢谢;)强>
堆栈跟踪表明您正在直接调用python方法,而不是udf
remove_punct
是一个普通的Python函数,而punct_remove
是一个udf,可以用作withColumn
调用的第二个参数解决此问题的一种方法是在
withColumn
调用中使用punct_remove
而不是remove_punct
另一种减少Python函数与udf混淆的方法是使用
@udf
注释:相关问题 更多 >
编程相关推荐