PySpark SQL Functions | translate method
Start your free 7-days trial now!
PySpark SQL Functions' translate(~)
method replaces the specified characters by the desired characters.
Parameters
1. srcCol
| string
or Column
The column to perform the operation on.
2. matching
| string
The characters to be replaced.
3. replace
| string
The characters to replace matching
.
Return Value
A new PySpark Column.
Examples
Consider the following PySpark DataFrame:
+-----+---+| name|age|+-----+---+| Alex| 20|| Bob| 30||Cathy| 40|+-----+---+
Replacing characters in PySpark Column
Suppose we wanted to make the following character replacements:
A -> #e -> @o -> %
We can perform these character replacements using the translate(~)
method:
Note that we can obtain a new PySpark DataFrame with the translated column using the withColumn(~)
method:
+-----+---+| name|age|+-----+---+| #l@x| 20|| B%b| 30||Cathy| 40|+-----+---+
Finally, note that specifying less characters for the replace
parameter will result in the removal of the corresponding characters in matching
:
Here, the characters e
and o
are removed, while A
is replaced by #
.