使用 PySpark 删除和替换字符

Remove & replace characters using PySpark

我有一个数据框,我想删除所有括号并用两个连字符替换。

之前:

+------------+
|  dob_concat|
+------------+
|[1983][6][3]|
+------------+

之后:

+------------+
| dob_concat |
+------------+
| 1983-6-3   |
+------------+

您可以使用 regex_replace 内置函数,如下所示。

from pyspark.sql import functions as F
df.withColumn("dob_concat", F.regexp_replace(F.regexp_replace(F.regexp_replace("dob_concat", "\]\[", "-"), "\[", ""), "\]", "")).show()