How to use flatMap() function in PySpark Azure Databricks?

Are you looking to find how to use flatMap() function in PySpark RDD Azure Databricks cloud or maybe you are looking for a solution, to do transformation using flatMap() on RDD in PySpark Databricks using the map methods? If you are looking for any of these problem solutions, then you have landed on the correct page. I will also show you what and how to use PySpark flatMap() function in PySpark Azure Databricks. I will explain it by taking a practical example. So don’t waste time let’s start step by step guide to understanding how to select columns in PySpark DataFrame.

In this blog, I will teach you the following with practical examples:

  • Syntax of flatMap()
  • Using flatMap() on RDD

flatMap() is a transformation used to apply the transformation function (lambda) on every element of RDD/DataFrame and returns a new RDD and then flattening the results.

Syntax: dataframe_name.flatMap()

What is the syntax of the flatMap() function in PySpark Azure Databricks?

The syntax is as follows:

flatMap(f, preservesPartitioning=False)
Parameter NameRequiredDescription
f (function)YesIt represents the function to be applied.
preservesPartitioning (bool)OptionalIt preserves partitioning.
Table 1: flatMap() Method in PySpark Databricks Parameter list with Details

Apache Spark Official Documentation Link: flatMap()

Create a simple RDD

Gentle reminder:

In Databricks,

  • sparkSession made available as spark
  • sparkContext made available as sc

In case, you want to create it manually, use the below code.

from pyspark.sql.session import SparkSession

spark = SparkSession.builder 

sc = spark.sparkContext

a) Create manual PySpark RDD

data = [
    "This is a sample line",
    "This is another sample line",
    "Again a sample line"

rdd = spark.sparkContext.parallelize(data)

['This is a sample line', 'This is another sample line', 'Again a sample line']

b) Creating an RDD by reading files

Download and use the below source file.

# replace the file_path with the source file location which you have downloaded.

rdd_2 = sc.textFile(file_path)

['This is a sample line', 'This is another sample line', 'Again a sample line']

Note: Here, I will be using the manually created RDD.

How to use the flatMap() function on RDD in PySpark Azure Databricks?

The PySpark flatMap() is a function that returns a new RDD by flattening the outcomes after applying a function to all of the items in this RDD.


rdd.flatMap(lambda line: line.lower().split(" ")).collect()




The reason for calling the flatMap a one-to-many operation is explained with an example below.

print(f"Number of inputs: {rdd.count()}")

rdd2 = rdd.flatMap(lambda line: line.lower().split(" "))
print(f"Number of outputs: {rdd2.count()}")


Number of inputs: 3
Number of outputs: 14


I have attached the complete code used in this blog in notebook format to this GitHub link. You can download and import this notebook in databricks, jupyter notebook, etc.

When should you use the flatMap() function PySpark in Azure Databricks?

When you want to flatten all the elements in an RDD, use the flatMap() transformation.

Real World Use Case Scenarios for flatMap() function in PySpark Azure Databricks?

Assume that you have a text file full of random words, for example (“This is a sample text 1”), (“This is a sample text 2”) and you have asked to find the word count. You can use the flatMap() function which flattens all the collections into a single collection. Chain this function with the map() function and pass 1 to it for finding the word count.

What are the alternative methods for iterating records in PySpark Azure Databricks?

There are multiple alternative methods for iterating records in PySpark Azure Databricks, which are as follows:

  • map() is a one-to-one operation which returns the record
  • flatMap() is a one-to-many operation that flattens all the collections into a single collection
  • foreach() is an iterating function but it would return anything

Final Thoughts

In this article, we have learned about the PySpark flatMap() transformation in Azure Databricks along with the examples explained clearly. I have also covered different scenarios with practical examples that could be possible. I hope the information that was provided helped in gaining knowledge.

Please share your comments and suggestions in the comment section below and I will try to answer all your queries as time permits.

PySpark in Azure Databricks, as explained by Arud Seka Berne S on

As a big data engineer, I design and build scalable data processing systems and integrate them with various data sources and databases. I have a strong background in Python and am proficient in big data technologies such as Hadoop, Hive, Spark, Databricks, and Azure. My interest lies in working with large datasets and deriving actionable insights to support informed business decisions.