Skip to main content

replace

Replaces all occurrences of search with replace.

For the corresponding Databricks SQL function, see replace function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.replace(src=<src>, search=<search>, replace=<replace>)

Parameters

Parameter

Type

Description

src

pyspark.sql.Column or str

A column of string to be replaced.

search

pyspark.sql.Column or str

A column of string, If search is not found in str, str is returned unchanged.

replace

pyspark.sql.Column or str, optional

A column of string, If replace is not specified or is an empty string, nothing replaces the string that is removed from str.

Examples

Python
df = spark.createDataFrame([("ABCabc", "abc", "DEF",)], ["a", "b", "c"])
df.select(replace(df.a, df.b, df.c).alias('r')).collect()
Output
[Row(r='ABCDEF')]
Python
df.select(replace(df.a, df.b).alias('r')).collect()
Output
[Row(r='ABC')]