Remove Punctuation in PySpark

Learn how to remove punctuation from text columns in PySpark using regexp_replace, Spark tokenizers, and safe DataFrame transformations.

Before you start

Punctuation removal in PySpark is a local DataFrame transformation. It does not require a camera, microphone, or media-processing service. You need a PySpark session with the target text column already loaded into a DataFrame. The process works on distributed string data, so the same expression can run on a driver-only test DataFrame or on a cluster DataFrame.

Check which Spark version you are using if you plan to combine punctuation removal with tokenization. The regexp_replace function and the default whitespace tokenizer are available across current PySpark versions, but tokenizer behavior can differ if you use custom patterns or non-default parameters.

Step-by-step instructions

Start by importing the functions you need. The main function for removing punctuation is regexp_replace from pyspark.sql.functions.

  • Import regexp_replace and col from pyspark.sql.functions.
  • Select the text column you want to clean.
  • Apply regexp_replace with a pattern that matches punctuation characters.
  • Alias the result as a new column or overwrite the original column.
  • Show or write the transformed DataFrame.

A practical pattern for English text is [^\w\s]. It matches any character that is not a word character or whitespace. In Python string syntax, write the pattern as r"[^\w\s]" and use an empty replacement string.

A basic example is: df.withColumn("clean_text", regexp_replace(col("text"), r"[^\w\s]", "")). This keeps letters, digits, underscores, and whitespace, while removing punctuation marks such as periods, commas, exclamation points, and question marks.

If you want to remove only ASCII punctuation without changing accented letters or other non-ASCII text, use a more specific class such as r"[!"#$%&'()*+,-./:;<=>?@[]^_`{|}~]". The broad pattern with \w can behave differently when text contains accented characters, so choose the pattern that matches your input.

Spark relies on Java regular expressions. Escape characters that have meaning inside a character class, and use a prefix such as r before the Python string so that backslashes reach the regular expression engine unchanged.

To use a tokenizer after cleaning, import Tokenizer from pyspark.ml.feature. The default tokenizer splits on whitespace, so punctuation removal can help produce cleaner tokens from strings like "hello, world!" when compare token results before and after cleaning.

Check the output

Inspect a few rows with df.select("text", "clean_text").show(truncate=False). For an input such as "Spark: fast, general!" the cleaned result should have "Spark fast general" or a similar string depending on your pattern. Reversibility depends on your DataFrame design: adding a new column keeps the original text available in the same version of the DataFrame.

Null values remain null because regexp_replace does not convert null to an empty string. If missing text should become an empty string, handle nulls separately with fillna or coalesce before making that assumption.

Common problems and next steps

If punctuation does not disappear, check whether the regular expression is being escaped twice by Python and Java. Using an r-prefixed string reduces that risk. Also compare the input encoding because non-ASCII punctuation may not match an ASCII-only pattern.

When working with a spark tokenizer, do not assume that tokenization removes punctuation automatically. The default PySpark Tokenizer splits on whitespace, so punctuation attached to a word can remain part of the token. Clean the text first if punctuation-free tokens are required.

For repeated use, place the pattern and column name in a reusable function and test it against a small DataFrame before applying it to a large table. Avoid inspecting the entire cluster output as the only verification step; a bounded preview plus a count comparison is usually enough to confirm the transformation ran.