How to convert PySpark DataFrame into Pandas DataFrame in Azure Databricks?

Are you looking to find out how to convert PySpark DataFrame into Pandas DataFrame in Azure Databricks cloud or maybe you are looking for a solution, to create Pandas DataFrame out of PySpark DataFrame using Azure Databricks? If you are looking for any of these problem solutions, you have landed on the correct page. I will also help you how to use the PySpark toPandas() function with multiple examples in Azure Databricks. I will explain it with a practical example. So please don’t waste time; let’s start with a step-by-step guide to understand how to use toPandas() function in PySpark.

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

  • Syntax of toPandas() function
  • Convert PySpark DataFrame to Pandas DataFrame without schema
  • Convert PySpark DataFrame to Pandas DataFrame with schema

The PySpark toPandas() function is used to convert a PySpark DataFrame into a Pandas DataFrame.

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

The syntax is as follows:

data_frame.toPandas()

Apache Spark Official documentation link: toPandas()

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 
    .master("local[*]") 
    .appName("azurelib.com") 
    .getOrCreate()

sc = spark.sparkContext

Before getting started, let’s create an RDD which will be helpful in the upcoming code samples.

columns = ["name", "dept", "salary"]
employee_data = [
    ("Kumar", "Sales", 25000),
    ("Shankar", "IT", 32000),
    ("Kavitha", "HR", 27000)
]

rdd = sc.parallelize(employee_data)
rdd.collect()

"""
Output:

[('Kumar', 'Sales', 25000), ('Shankar', 'IT', 32000), ('Kavitha', 'HR', 27000)]

"""

How to convert PySpark DataFrame into Pandas DataFrame in Azure Databricks using the toPandas() function?

Let’s see how to convert a PySpark DataFrame into Pandas DataFrame in Azure Databricks using the toPandas() function.

Example:

# a) without column name
df1 = rdd.toDF()
print(df1.toPandas())

"""
Output:

        _1     _2     _3
0    Kumar  Sales  25000
1  Shankar     IT  32000
2  Kavitha     HR  27000

"""

As you can see, when we don’t specify any column names, PySpark will refer to the column as _1, _2, etc. In order to specify the schema pass column names using the ‘schema’ parameter.

# b) Using toDF() with column name
df2 = rdd.toDF(schema=columns)
print(df2.toPandas())

"""
Output:

      name   dept  salary
0    Kumar  Sales   25000
1  Shankar     IT   32000
2  Kavitha     HR   27000

"""

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

When should you convert PySpark DataFrame to Pandas DataFrame in Azure Databricks?

PySpark is faster than Pandas, because of parallel execution and processing. Therefore, we use a PySpark DataFrame. For further processing using machine learning tools or any Python applications, we would need to convert the data back to Pandas DataFrame after processing it with PySpark.

Real World Use Case Scenarios for converting PySpark DataFrame to Pandas DataFrame in Azure Databricks?

The Pandas DataFrame will be highly beneficial in processing the data that runs on a single machine, assuming you are required to analyze the data further using any machine learning tools or any other Python applications that operate on a single node.

What are the alternatives for converting PySpark DataFrame to Pandas DataFrame in Azure Databricks?

The PySpatk toPandas() function is the only one that helps in converting a Python DataFrame into a Pandas DataFrame.

Final Thoughts

In this article, we have learned about the conversion of PySpark DataFrame to Pandas DataFrame 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 azurelib.com.

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.