0

I'm trying to replace the "/" character with space(" ") from data in a column called UserAgent in a dataframe df_test

Data in the column looks like this:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

I have tried using

val df_test =spark.sql(s"select UserAgent from df_header_pivot")
 df_test.withColumn("UserAgent", regexp_replace("UserAgent", "[/]", ""))

but I'm getting error message:

notebook:4: error: overloaded method value regexp_replace with alternatives: (e: org.apache.spark.sql.Column,pattern: org.apache.spark.sql.Column,replacement: org.apache.spark.sql.Column)org.apache.spark.sql.Column (e: org.apache.spark.sql.Column,pattern: String,replacement: String)org.apache.spark.sql.Column cannot be applied to (org.apache.spark.sql.ColumnName, org.apache.spark.sql.Column) df_test.withColumn("UserAgent", regexp_replace($"UserAgent" , lit("/")))

2 Answers 2

2

You need to use the $ symbol before the column name in regexp_replace function. import org.apache.spark.sql.functions._ val df_test =spark.sql(s"select UserAgent from df_header_pivot") df_test.withColumn("UserAgent", regexp_replace($"UserAgent", "[/]", " "))

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

1 Comment

Nice answer Vijay!
1

As you are using SparkSQL, you could simply call the replace function in the SQL itself, eg something like this:

val df_test =spark.sql(s"select replace(UserAgent, '/', '') AS UserAgent from tmp")

df_test.show

Alternately use the translate function to replace characters where no regex is required, eg

df_test
  .withColumn("UserAgent", translate($"UserAgent", "/", ""))
  .show

No regex required.

2 Comments

Hey wBob, it is possible in scala/python also ?
Hey @VijayKumarSharma, check my answer with updated use of translate function. HTH

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.