Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

add character at character count in pyspark

looking to insert special character at specific character count in pyspark string –

"M202876QC0581AADMM01"
to
"M-202876-QC0581-AA-DMM01"

(1-6-6-2-)
insertion after 1char then after 6char then after 6char then after 2char

Tried something like below but now luck yet.

df = spark.createDataFrame([('M202876QC0581AADMM01',)], ['str'])
(df.withColumn("str", F.regexp_replace(F.col("str") ,  r"(\d{0})(\d{3})(\d{3})" , "$1-$2-$3"))).collect()

Out[121]: [Row(str='M-202-876QC0581AADMM01')]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re close, try this :

from pyspark.sql.functions import regexp_replace

df = spark.createDataFrame([("M202876QC0581AADMM01",)], ["str"])

pat = r"^(.{1})(.{6})(.{6})(.{2})(.+)"
df = df.withColumn("str", regexp_replace("str", pat, r"$1-$2-$3-$4-$5"))

Output :

df.show(truncate=False)

+------------------------+
|str                     |
+------------------------+
|M-202876-QC0581-AA-DMM01|
+------------------------+
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading