【问题标题】:Removing punctuation in spark dataframe删除火花数据框中的标点符号
【发布时间】:2019-09-21 09:22:19
【问题描述】:

我正在尝试使用正则表达式从我的标记化文本中删除标点符号。我正在使用火花数据框。 这是我的功能:

def removePunctuation(column):
     return trim(lower(regexp_replace(column,'[^\sa-zA-Z0-9]', ''))).alias('stopped')

当我通过以下方式执行此功能时:

removed_df.select(removePunctuation(col('stopped'))).show(truncate=False)

我有错误:

Py4JJavaError: An error occurred while calling o736.select.
: org.apache.spark.sql.AnalysisException: cannot resolve 'regexp_replace(`stopped`, '[^\\sa-zA-Z0-9]', '')' due to data type mismatch: argument 1 requires string type, however, '`stopped`' is of array<string> type.;;

有没有办法通过这个函数来删除标点符号?它有什么问题?

【问题讨论】:

    标签: dataframe pyspark punctuation


    【解决方案1】:

    错误消息显示您的列stopped 的类型为array&lt;string&gt; 而不是string。您需要regexp_replace 的字符串列。

    为了将 if 应用于字符串数组,您可以先从数组中创建一个字符串,然后再次拆分该字符串

    def removePunctuation(column):
         return split(trim(lower(regexp_replace(concat_ws("SEPARATORSTRING", column),'[^\sa-zA-Z0-9]', ''))), "SEPARATORSTRING").alias('stopped')
    
    

    【讨论】:

    • 当然可以,但是怎么做呢?这是我的数据框列,我可以简单地更改字符串的类型吗?
    • 您需要先连接然后在分隔符上拆分数组。查看修改后的答案
    • 我得到了这样的东西:TypeError: Invalid argument, not a string or column: of type 。对于列文字,请使用“lit”、“array”、“struct”或“create_map”函数。
    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 2018-09-19
    • 2020-10-04
    • 2021-12-18
    • 2016-04-10
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    相关资源
    最近更新 更多