Apache Spark Python - Processing Column Data - Extracting Strings using substring

In this article, we will delve into the concept of extracting strings from a main string using the substring function in Pyspark. This is particularly useful when dealing with fixed-length columns and extracting specific information from them.

Fixed-length Columns

When we are processing fixed-length columns, we often use the substring function to extract relevant information. Some common examples of fixed-length columns include a 9-digit Social Security Number or a 16-digit Credit Card Number. We typically extract specific parts of these strings for various purposes.

The substring Function

The substring function in Pyspark takes 3 arguments - the column from which to extract the substring, the starting position, and the length of the substring. Additionally, the starting position can be provided from the end of the string by passing a negative value.

To see the practical demonstration and walkthrough of using the substring function in Pyspark, watch the video provided below.

Watch the video tutorial here

Hands-On Tasks

Let’s perform a few tasks to extract information from fixed-length strings:

  1. Create a list of employees with columns for name, SSN, and phone number.
  2. Define the formats for SSN and Phone Number, detailing the structure of each.
  3. Create a DataFrame with the specified columns.
  4. Extract the last 4 digits from the phone number for each employee.
  5. Extract the last 4 digits from the SSN for each employee.


In summary, the substring function in Pyspark is a powerful tool for extracting specific information from fixed-length columns. By understanding its usage and applying it in practical scenarios, you can efficiently manipulate string data in your Spark applications.

Now that you understand how to extract strings using the substring function in Pyspark, try out the hands-on tasks and explore further applications of this concept. Practice is key to mastery, so don’t hesitate to experiment with different scenarios.

Remember to engage with the community for additional support and learning opportunities. Join the discussion to deepen your understanding and share your insights with fellow enthusiasts.

Stay curious and keep exploring the world of Pyspark!