【问题标题】:Regex pattern to remove numeric value from words in pyspark正则表达式模式从 pyspark 中的单词中删除数值
【发布时间】:2023-03-05 10:58:02
【问题描述】:

我正在研究 pyspark 数据框,我有一列 words (array<string> type)。从单词中删除数值和数值的正则表达式模式应该是什么?

+---+----------------------------------------------+
|id |    words                                     |
+---+----------------------------------------------+
|564|[fhbgtrj5, 345gjhg, ghth578ghu, 5897, fhrfu44]|
+---+----------------------------------------------+

预期输出:

+---+----------------------------------------------+
|id |words                                         |
+---+----------------------------------------------+
|564|               [fhbgtrj, gjhg, ghthghu, fhrfu]|
+---+----------------------------------------------+

请帮忙。

【问题讨论】:

  • 这能回答你的问题吗? Delete digits in Python (Regex)
  • @jbflow 感谢您的调查。您分享的参考文献肯定会删除数字,但另一个目的是防止字母数字使用字母

标签: python apache-spark pyspark apache-spark-sql


【解决方案1】:

您可以使用transformregexp_replace 一起删除数字,并使用array_remove 删除空条目(来自那些仅由数字组成的条目)。

df2 = df.withColumn(
    'words', 
    F.expr("array_remove(transform(words, x -> regexp_replace(x, '[0-9]', '')), '') as words")
)

df2.show(truncate=False)
+---+-------------------------------+
|id |words                          |
+---+-------------------------------+
|564|[fhbgtrj, gjhg, ghthghu, fhrfu]|
+---+-------------------------------+

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2020-01-25
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多