search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

PySpark SQL Functions | regexp_replace method

schedule Aug 12, 2023
Last updated
local_offer
PySpark
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
3
thumb_down
2
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!