pyspark.sql.functions.translate#

pyspark.sql.functions.translate(srcCol, matching, replace)[source]#

A function translate any character in the srcCol by a character in matching. The characters in replace is corresponding to the characters in matching. Translation will happen whenever any character in the string is matching with the character in the matching.

Added in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters:
srcColColumn or column name

Source column or strings. A column that evaluates to a string.

matchingstr

matching characters. A column that evaluates to a string.

replacestr

characters for replacement. If this is shorter than matching string then those chars that don’t have replacement will be dropped. A column that evaluates to a string.

Returns:
Column

replaced value. Returns a column that evaluates to a string.

Examples

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('translate',)], ['a'])
>>> df.select('*', sf.translate('a', "rnlt", "123")).show()
+---------+-----------------------+
|        a|translate(a, rnlt, 123)|
+---------+-----------------------+
|translate|                1a2s3ae|
+---------+-----------------------+