How can I replace multiple characters from all columns of a spark dataframe?

Viewed 9043

I have a dataframe containing multiple columns.

>>> df.take(1)
[Row(A=u'{dt:dt=string, content=Prod}', B=u'{dt:dt=string, content=Staging}')]

I want to remove both curly braces '{' and '}' from values of column A and B of df. I know we can use:

df.withColumn('A',regexp_replace('A','//{',''))
df.withColumn('A',regexp_replace('A','//}',''))
df.withColumn('B',regexp_replace('B','//}',''))

How do I replace characters dynamically for all columns of Spark dataframe? (Pandas version is shown below)

df = df.replace({'{':'','}':''},regex=True)
2 Answers

Just use proper regular expression:

df.withColumn("A", regexp_replace("A", "[{}]", ""))

To modify a dataframe df and apply regexp_replace to multiple columns given by listOfColumns you could use foldLeft like so:

val newDf = listOfColumns.foldLeft(df)((acc, x) => acc.withColumn(x, regexp_replace(col(x), ..., ...)))
Related