pyspark:删除作为另一列值的子字符串,并包含给定列值中的正则表达式字符

标签 pyspark databricks azure-databricks

假设我有一个像这样的数据框

df = spark.createDataFrame(
  [
    ('Test1 This is a test Test2','This is a test'),
    ('That is','That')
  ],
  ['text','name'])


+--------------------------+--------------+
|text                      |name          |
+--------------------------+--------------+
|Test1 This is a test Test2|This is a test|
|That is                   |That          |
+--------------------------+--------------+

如果我应用 df.withColumn("new",F.expr("regexp_replace(text,name,'')")).show(truncate=False) 它工作正常并且结果在

+--------------------------+--------------+------------+
|text                      |name          |new         |
+--------------------------+--------------+------------+
|Test1 This is a test Test2|This is a test|Test1  Test2|
|That is                   |That          | is         |
+--------------------------+--------------+------------+

假设我有以下数据框

+-----------------------------+-----------------+
|text                         |name             |
+-----------------------------+-----------------+
|Test1 This is a test(+1 Test2|This is a test(+1|
|That is                      |That             |
+-----------------------------+-----------------+

如果我从上面应用命令,我会收到以下错误消息:

java.util.regex.PatternSyntaxException: Dangling meta character '+&#39

我该怎么做才能使此异常不会以最“pyspark”的方式发生并保持文本中的值不变?

谢谢

最佳答案

在 spark 中使用 replace 函数代替 regexp_replace

replace(str, search[, replace]) - Replaces all occurrences of search with replace.

示例:

df.show(10,False)
#+-----------------------------+-----------------+
#|text                         |name             |
#+-----------------------------+-----------------+
#|Test1 This is a test(+1 Test2|This is a test(+1|
#|That is                      |That             |
#+-----------------------------+-----------------+

df.withColumn("new",expr("replace(text,name,'')")).show(10,False)
#+-----------------------------+-----------------+------------+
#|text                         |name             |new         |
#+-----------------------------+-----------------+------------+
#|Test1 This is a test(+1 Test2|This is a test(+1|Test1  Test2|
#|That is                      |That             | is         |
#+-----------------------------+-----------------+------------+

关于pyspark:删除作为另一列值的子字符串,并包含给定列值中的正则表达式字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65022464/

相关文章:

json - 使用 spark-submit 从 google dataproc spark cluster 读取 GCP 中的 JSON(zipped .gz) 时,未使用所有执行程序

python - 将尚未存在于另一个数据框中的列添加到数据框中

python - PySpark 窗口函数理解

python - 如何从 Azure Databricks Spark 中的 DataFrame 获取特定行和列

c# - 如何从 Azure 数据工厂在 Databricks 上运行 .Net spark 作业?

azure - SSL错误: HTTPSConnectionPool when registering ML model on Azure

lambda - spark 映射中的 lambda 表达式中的 * 是什么意思?

apache-spark - 如何在 Databricks 中使用 Spark 将 JSON 文件并行写入到已安装的目录

azure - Databricks CLI,终止集群

python - 检查Databricks中是否存在该路径