How Do You Check Data Type in DataFrame PySpark?


Angela Bailey

When working with data in PySpark, it’s essential to be able to check the data type of columns in a DataFrame. This information can help you understand the structure and properties of your data, which is crucial for performing various transformations and analyses. In this tutorial, we will explore different ways to check the data type in a DataFrame using PySpark.

Using the ‘printSchema()’ Method

The most straightforward way to check the data type of columns in a DataFrame is by using the printSchema() method. This method displays the schema of the DataFrame, which includes the column names and their corresponding data types.

Here’s an example:


This will output something like:

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

In this example, we can see that the ‘name’ column has a data type of string, the ‘age’ column has a data type of integer, and the ‘salary’ column has a data type of double.

Using the ‘dtypes’ Attribute

An alternative way to check the data types in a DataFrame is by using the dtypes attribute. This attribute returns a list of tuples, where each tuple contains the name of a column and its corresponding data type.

To access this information, you can use code like this:

column_types = df.dtypes
for col_name, col_type in column_types:
    print(col_name + ": " + col_type)

Using the ‘select()’ Method

The select() method in PySpark allows you to select specific columns from a DataFrame. By chaining this method with the dtypes attribute, you can retrieve the data types of specific columns.'name', 'age').dtypes

This will return a list of tuples containing the data types for the ‘name’ and ‘age’ columns.


In this tutorial, we explored different methods for checking data types in a PySpark DataFrame. We learned how to use the printSchema() method to display the schema of a DataFrame, how to access the dtypes attribute to retrieve a list of column names and data types, and how to use the select() method to get the data types of specific columns.

The ability to check data types is essential when working with PySpark, as it allows you to gain insights into your data and perform accurate transformations and analyses. By incorporating these techniques into your workflow, you’ll be better equipped to handle complex datasets and extract meaningful information.

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

Privacy Policy