How to display DataFrames in PySpark Azure Databricks?

Are you looking to find out how to show DataFrames in PySpark using Azure Databricks cloud or maybe you are looking for a solution, to display DataFrame in PySpark using show() function? 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 show() 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 show() function in PySpark.

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

  • Syntax of show()
  • Limiting DataFrame records
  • Display DataFrame vertically
  • Truncating columns of DataFrame

The PySpark show() function is used to display or print records. By default the show() function prints 20 records.

data_frame.show()

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

The syntax is as follows:

data_frame.show(limit, vertical, truncate)
Parameter NameRequiredDescription
limit (int)OptionalIt represents the number of records to be displayed. By default, it was set to 20.
vertical (bool)OptionalIt prints each record in a vertical manner.
truncate (bool, int)OptionalIt controls text overflow. By default, it truncates the text longer than 20.
Table 1: show() Method in PySpark Databricks Parameter list with Details

Apache Spark Official documentation link: show()

Create a simple DataFrame

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

a) Create manual PySpark DataFrame

data = [  
    (1,"Nessie","Kesten","Physicians Total Care, Inc."),
    (2,"Giordano","Loseke","Concept Laboratories, Inc."),
    (3,"Issie","MacLeese","Conopco Inc. d/b/a Unilever"),
    (4,"Rozina","Fursse","Acura Pharmaceuticals, Inc."),
    (5,"Robers","Doody","Aqua Pharmaceuticals")
]

df = spark.createDataFrame(data, schema=["id", "first_name", "last_name", "company"])
df.printSchema()
df.show(truncate=False)

"""
root
 |-- id: long (nullable = true)
 |-- first_name: string (nullable = true)
 |-- last_name: string (nullable = true)
 |-- company: string (nullable = true)

+---+----------+---------+---------------------------+
|id |first_name|last_name|company                    |
+---+----------+---------+---------------------------+
|1  |Nessie    |Kesten   |Physicians Total Care, Inc.|
|2  |Giordano  |Loseke   |Concept Laboratories, Inc. |
|3  |Issie     |MacLeese |Conopco Inc. d/b/a Unilever|
|4  |Rozina    |Fursse   |Acura Pharmaceuticals, Inc.|
|5  |Robers    |Doody    |Aqua Pharmaceuticals       |
+---+----------+---------+---------------------------+
"""

b) Creating a DataFrame by reading files

Download and use the below source file.

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

df_2 = spark.read.format("csv").option("header", True).load(file_path)
df_2.printSchema()

"""
root
 |-- id: long (nullable = true)
 |-- first_name: string (nullable = true)
 |-- last_name: string (nullable = true)
 |-- company: string (nullable = true)
"""

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

How to limit the number of records in PySpark DataFrame using Azure Databricks?

Let’s see how to limit the number of records in PySpark DataFrame in Azure Databricks using the show() function.

Example 1:

df.show(truncate=False)

"""
Output:

+---+----------+---------+---------------------------+
|id |first_name|last_name|company                    |
+---+----------+---------+---------------------------+
|1  |Nessie    |Kesten   |Physicians Total Care, Inc.|
|2  |Giordano  |Loseke   |Concept Laboratories, Inc. |
|3  |Issie     |MacLeese |Conopco Inc. d/b/a Unilever|
|4  |Rozina    |Fursse   |Acura Pharmaceuticals, Inc.|
|5  |Robers    |Doody    |Aqua Pharmaceuticals       |
+---+----------+---------+---------------------------+

"""

As you can see, by default the show() function displays 20 records.

Example 2:

df.show(n=3, truncate=False)

"""
Output:

+---+----------+---------+---------------------------+
|id |first_name|last_name|company                    |
+---+----------+---------+---------------------------+
|1  |Nessie    |Kesten   |Physicians Total Care, Inc.|
|2  |Giordano  |Loseke   |Concept Laboratories, Inc. |
|3  |Issie     |MacLeese |Conopco Inc. d/b/a Unilever|
+---+----------+---------+---------------------------+

"""

By using the n parameter we can control the number of records to be displayed.

How to show each record vertically in PySpark DataFrame using Azure Databricks?

Let’s see how to show each record vertically in PySpark DataFrame in Azure Databricks using the show() function.

Example:

df.show(n=2, vertical=True)

"""
Output:

-RECORD 0--------------------------
 id         | 1                    
 first_name | Nessie               
 last_name  | Kesten               
 company    | Physicians Total ... 
-RECORD 1--------------------------
 id         | 2                    
 first_name | Giordano             
 last_name  | Loseke               
 company    | Concept Laborator... 

"""

As you can see, by default the vertical parameter is set to False. By Enabling it we can print each record in a vertical fashion.

How to truncate column values of PySpark DataFrame in Azure Databricks using show() function?

Let’s see how to truncate column values of PySpark DataFrame in Azure Databricks using the show() function.

Example 1:

df.show(n=5, truncate=True)

"""
Output:

+---+----------+---------+--------------------+
| id|first_name|last_name|             company|
+---+----------+---------+--------------------+
|  1|    Nessie|   Kesten|Physicians Total ...|
|  2|  Giordano|   Loseke|Concept Laborator...|
|  3|     Issie| MacLeese|Conopco Inc. d/b/...|
|  4|    Rozina|   Fursse|Acura Pharmaceuti...|
|  5|    Robers|    Doody|Aqua Pharmaceuticals|
+---+----------+---------+--------------------+

"""

As you can see, the company column values get truncated. This happens because the truncated parameter is set to True by default. We can either change it to False to avoid truncating the columns or set a numeric value to truncate column furthur.

Example 2:

df.show(n=5, truncate=False)

"""
Output:

+---+----------+---------+---------------------------+
|id |first_name|last_name|company                    |
+---+----------+---------+---------------------------+
|1  |Nessie    |Kesten   |Physicians Total Care, Inc.|
|2  |Giordano  |Loseke   |Concept Laboratories, Inc. |
|3  |Issie     |MacLeese |Conopco Inc. d/b/a Unilever|
|4  |Rozina    |Fursse   |Acura Pharmaceuticals, Inc.|
|5  |Robers    |Doody    |Aqua Pharmaceuticals       |
+---+----------+---------+---------------------------+

"""

Now, you can see the full name of the company.

Example 3:

df.show(n=5, truncate=10)

"""
Output:

+---+----------+---------+----------+
| id|first_name|last_name|   company|
+---+----------+---------+----------+
|  1|    Nessie|   Kesten|Physici...|
|  2|  Giordano|   Loseke|Concept...|
|  3|     Issie| MacLeese|Conopco...|
|  4|    Rozina|   Fursse|Acura P...|
|  5|    Robers|    Doody|Aqua Ph...|
+---+----------+---------+----------+

"""

The column values are further truncated.

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 use show() function of PySpark in Azure Databricks?

These could be the possible reasons:

  • To limit the number of records to be displayed
  • To view the transformation, you made to your DataFrame
  • To run code, because without action the code execution will not happen

Real World Use Case Scenarios for using show() function of PySpark in Azure Databricks?

  • The situation where you want to display only 10 records.
  • In the situation, where you need to display 100 records, by default show() function, displays the top 20 records.
  • Situations where you want to see the transformation applied on a DataFrame.

What are the alternatives for displaying DataFrames in Azure Databricks?

There are multiple alternatives for displaying PySpark DataFrames, which are as follows:

  • collect(): is an action used for fetching the result as collections of Rows
  • take(): is an action that takes a number of records to be displayed as a collection of Row

Final Thoughts

In this article, we have learned about displaying PySpark DataFrames 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.