【问题标题】:Remove special character from array pyspark从数组pyspark中删除特殊字符
【发布时间】:2020-04-22 21:17:30
【问题描述】:

我有一个 pyspark 数据框,其中包含一列字符串。

df 示例:

number | id
---------------
12     | [12, .AZ, .UI]
------------------------
14     | [CL, .RT, OP.]

我要删除字符'.'

我尝试使用regexp_replace

df = df.select("id", F.regexp_replace(F.col("id"), ".").alias("id"))

但我认为 regexp_replace 是字符串而不是数组的好解决方案。

如何从数组中删除此字符? 谢谢你

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    在 Spark 2.4 或更高版本中你可以使用你可以使用transform

    import pyspark.sql.functions as F
    df.withColumn("id",F.expr("transform(id,x-> replace(x,'.',''))")).show()
    

    +------+------------+
    |number|          id|
    +------+------------+
    |    12|[12, AZ, UI]|
    |    14|[CL, RT, OP]|
    +------+------------+
    

    工作示例:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2011-04-11
      • 2016-01-23
      相关资源
      最近更新 更多