How Do I Check My Spark DF Data Type?


Angela Bailey

How Do I Check My Spark DF Data Type?

Apache Spark is a powerful distributed computing framework that provides an interface for programming entire clusters with implicit data parallelism and fault tolerance. When working with Spark, it’s essential to understand the data types of the DataFrame columns.

In this tutorial, we will explore different methods to check the data types of a Spark DataFrame.

Using the printSchema() Method

One of the simplest ways to check the data types of a Spark DataFrame is by using the printSchema() method. This method displays the schema of the DataFrame, including column names and their corresponding data types.

Here’s an example:


The output will resemble something like this:

 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- salary: double (nullable = true)

As seen in the output, each column is displayed along with its data type in parentheses. The nullable flag indicates whether a column can contain null values or not.

Using dtypes Property

Another way to check the data types of a Spark DataFrame is by accessing the dtypes property. The dtypes property returns an Array[(String, String)], where each element represents a column name and its corresponding data type.

Here’s an example usage:


The output will be an array similar to this:

  ("name", "StringType"),
  ("age", "IntegerType"),
  ("salary", "DoubleType")

As you can see, each column is represented by a tuple containing the column name and its data type. The data types are represented as strings.

Using the schema Property

The schema property of a Spark DataFrame provides access to the schema in a structured manner. It returns an instance of StructType, which represents the DataFrame schema as a tree-like structure.


The output will be similar to this:


In this representation, each field is represented by an instance of StructField. The name, age, and salary are the column names, and their respective data types are shown as well.

In Conclusion:

Checking the data types of Spark DataFrame columns is a crucial step in data analysis and manipulation. In this tutorial, we explored different methods to accomplish this task.

The printSchema() method provides a readable overview of the schema, while the dtypes property and the schema property offer more structured access to individual column names and data types.

By using these methods effectively, you can gain valuable insights into your data and ensure that your Spark applications handle the correct data types efficiently.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy