search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

PySpark SQL Functions | regexp_replace method

Machine Learning
chevron_right
PySpark
chevron_right
Documentation
chevron_right
PySpark SQL Functions
schedule Jul 1, 2022
Last updated
local_offer PySpark
Tags

PySpark SQL Functions' regexp_replace(~) method replaces the matched regular expression with the specified string.

Parameters

1. str | string or Column

The column whose values will be replaced.

2. pattern | string or Regex

The regular expression to be replaced.

3. replacement | string

The string value to replace pattern.

Return Value

A new PySpark Column.

Examples

Consider the following PySpark DataFrame:

df = spark.createDataFrame([['Alex', 10], ['Mile', 30]], ['name', 'age'])
df.show()
+----+---+
|name|age|
+----+---+
|Alex| 10|
|Mile| 30|
+----+---+

Replacing a specific substring

To replace the substring 'le' with 'LE', use regexp_replace(~):

from pyspark.sql import functions as F
# Use an alias to assign a new name to the returned column
df.select(F.regexp_replace('name', 'le', 'LE').alias('new_name')).show()
+--------+
|new_name|
+--------+
| ALEx|
| MiLE|
+--------+
NOTE

The second argument is a regular expression, so characters such as $ and [ will carry special meaning. In order to treat these special characters as literal characters, escape them using the \ character (e.g. \$).

Passing in a Column object

Instead of referring to the column by its name, we can also pass in a Column object:

df.select(F.regexp_replace(df.name, 'le', 'LE').alias('new_name')).show()
+--------+
|new_name|
+--------+
| ALEx|
| MiLE|
+--------+

Getting a new PySpark DataFrame

We can use the PySpark DataFrame's withColumn(~) method to obtain a new PySpark DataFrame with the updated column like so:

df.withColumn('name', F.regexp_replace("name", 'le', 'LE').alias('new_name')).show()
+----+---+
|name|age|
+----+---+
|ALEx| 10|
|MiLE| 30|
+----+---+

Replacing a specific substring using regular expression

To replace the substring 'le' that occur only at the end with 'LE', use regexp_replace(~):

from pyspark.sql import functions as F
df.select(F.regexp_replace('name', 'le$', 'LE').alias('new_name')).show()
+--------+
|new_name|
+--------+
| Alex|
| MiLE|
+--------+

Here, we are using the special regular expression character '$' that only matches patterns occurring at the end of the string. This is the reason no replacement was done for the 'le' in Alex.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_down
1
thumb_down
1
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!