Get List of columns and its data type in Pyspark

In order to Get list of columns and its data type in pyspark we will be using dtypes function and printSchema() function . We will explain how to get list of column names and its data type in pyspark with an example.

  • Get List of columns in pyspark dataframe.
  • Get List of columns and its datatype

We have used two methods to get list of column name and its data type in Pyspark.

We will use the dataframe named df_basket1.

Get List of columns and its data type in Pyspark 1

 

Get List of columns in pyspark:

To get list of columns in pyspark we use dataframe.columns syntax

df_basket1.columns

So the list of columns will be

Get List of columns and its data type in Pyspark 2

 

Get list of columns and its data type in pyspark

Method 1:  using printSchema() function.

df_basket1.printSchema()

printSchema() function gets the data type of each column as shown below

Get List of columns and its data type in Pyspark 3

 

Method 2:  using dtypes function.

df_basket1.dtypes

dtypes function gets the data type of each column as shown below

Get List of columns and its data type in Pyspark 4

 

Get List of columns and its data type in Pyspark                                                                                               Get List of columns and its data type in Pyspark