Skip to content

LazyColumn.str.replace

replace

replace(old: str, new: str) -> LazyColumn

Replaces all occurrences of a substring within each string with a new value.

Parameters:

Name Type Description Default
old str

The substring to be replaced.

required
new str

The new string to replace occurrences of old.

required

Returns:

Name Type Description
LazyColumn LazyColumn

A new LazyColumn with all occurrences of old replaced by new.

Examples:

print(df.head())
#    col1 my_string_column
# 0     1          "Hello"
# 1     2          "Hello World"
# 2     3          "foo"
# 3     4          "bar"
# 4     5          None

df["my_string_column"].str.replace("Hello", "Hi")
# ["Hi", "Hi World", "foo", "bar", None]