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

Replacing certain substrings in PySpark DataFrame column

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!

To replace certain substrings in column values of a PySpark DataFrame, use either PySpark SQL Functions' translate(~) method or regexp_replace(~) method.

As an example, consider the following PySpark DataFrame:

df = spark.createDataFrame([["!A@lex"], ["B#!ob"]], ["name"])
df.show()
+------+
| name|
+------+
|!A@lex|
| B#ob|
+------+

Replacing certain characters

Suppose we wanted to make the following character replacements:

'!' replaced by '3'
'@' replaced by '4'
'#' replaced by '5'

We can use the translate(~) method like so:

from pyspark.sql import functions as F
df_new = df.withColumn("name", F.translate("name", "!@#", "345"))
df_new.show()
+------+
| new|
+------+
|3A4lex|
| B5ob|
+------+

The withColumn(~) here is used to replace the name column with our new column.

Replacing certain substrings

Consider the following PySpark DataFrame:

df = spark.createDataFrame([["A@@ex"], ["@Bob"]], ["name"])
df.show()
+-----+
| name|
+-----+
|A@@ex|
| @Bob|
+-----+

To replace certain substrings, use the regexp_replace(~) method:

from pyspark.sql import functions as F
df_new = df.withColumn("name", F.regexp_replace("name", "@@", "l"))
df_new.show()
+----+
|name|
+----+
|Alex|
|@Bob|
+----+

Here, note the following:

  • we are replacing the substring "@@" with the letter "l".

NOTE

The second argument of regexp_replace(~) is a regular expression. This means that certain characters such as $ and [ carry special meaning. To replace literal substrings, escape special regex characters using backslash \ (.g. \[).

Replacing certain substrings using Regex

Consider the following PySpark DataFrame:

df = spark.createDataFrame([["A@ex"], ["@Bob"]], ["name"])
df.show()
+----+
|name|
+----+
|A@ex|
|@Bob|
+----+

To replace @ if it's at the beginning of the string with another string, use regexp_replace(~):

from pyspark.sql import functions as F
df_new = df.withColumn("name", F.regexp_replace("name", "^@", "*"))
df_new.show()
+----+
|name|
+----+
|A@ex|
|*Bob|
+----+

Here, the regex ^@ represents @ that is at the start of the string.

Replacing certain substrings in multiple columns

The regexp_replace(~) can only be performed on one column at a time.

For example, consider the following PySpark DataFrame:

df = spark.createDataFrame([['@a','@b'], ['@c','@d']], ['A', 'B'])
df.show()
+---+---+
| A| B|
+---+---+
| @a| @b|
| @c| @d|
+---+---+

To replace the substring '@' with '#' for columns A and B:

str_before = '@'
str_after = '#'
df_new = df.withColumn('A', F.regexp_replace('A', str_before, str_after))
df_new = df_new.withColumn('B', F.regexp_replace('B', str_before, str_after))
df_new.show()
+---+---+
| A| B|
+---+---+
| #a| #b|
| #c| #d|
+---+---+
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
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!