When working with text data, replacing characters with other characters is common. However, most replacements are applied only once. For example, if you have “barb” and replace “b” with “z”, you’ll get “zarb” instead of “zarz”. How can you ensure that you end up with “zarz” in polars?
Below is a dataframe showing phone numbers.
shape: (3, 2)
Name
Phone No
str
str
"Serena van der Wooodsen"
"222 333 4444"
"Blair Waldorf"
"333 444 5555"
"Dan Humphrey"
"666 777 8888"
Replace space with dash
To replace all spaces in each phone number with dashes, use the polars expression replace_all as follows: