0

Let say that there is a Map with key value pair or text like:

val pairs = Map(
  "x1" -> "a",
  "1" -> "xf",
  "80" -> "AB"
)

Is there a way to add new column with regexp_replace invocation in cycle like that:

df.withColumn("newColumn", pairs.mapSomeHow((k,v) => regexp_replace(col("originalColumn"), k, v)))

E.g. newColumn will have value from originalColumn with "x1", "1", "80" strings replaced.

How to do that?

1 Answer 1

2

Something like

df.withColumn(
  "newColumn",
  pairs.foldLeft(df("originalColumn")) {
    case (c, (k, v)) =>
      regexp_replace(c, k, v)
  }
)

This will build a Column instance from the original df("originalColumn") instance by repeatedly applying regexp_replace to the result of the previous replace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.