Spark Check Column Present in DataFrame

Spread the love

You can get all columns of a DataFrame as an Array[String] by using columns attribute of Spark DataFrame and use this with Scala Array functions to check if a column/field present in DataFrame, In this article I will also cover how to check if a column present/exists in nested column and by case insensitive.

1. Spark Check if Column Exists in DataFrame

Spark DataFrame has an attribute columns that returns all column names as an Array[String], once you have the columns, you can use the array function contains() to check if the column present.

Note that df.columns returns only top level columns but not nested struct columns.

val columnNameToCheck="name"
  println("column exists")
  println("column not exists")

2. Check Column Exists by Case insensitive

Let’s check if a column exists by case insensitive, In order to check first convert the column name you wanted to check to CAPS & all DataFrame columns to Caps and compare both. This is a general trick to do case insensitive, you can also convert to the SMALL case instead of CAPS.

3. Check Column exists in Nested Struct DataFrame

df.columns don’t return columns from the nested struct, so If you have a DataFrame with nested struct columns, you can check if the column present on the nested column of Spark DataFrame by getting schema in a string using df.schema.simpleString().


4. Check if a Field Exists in a DataFrame

If you want to check if a Column exists with the same Data Type, then use the Spark schema functions df.schema.fieldNames or df.schema.contains().

import org.apache.spark.sql.types.{StringType, StructField}

5. Complete Example of How to Check Column Presents in Spark DataFrame

Following is complete example for reference.

import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SparkSession}

object ColumnExists extends App {

  val spark:SparkSession = SparkSession.builder()

  val data = Seq(Row("James",Row("black","blue")),
    Row("Ann",Row("grey ","black"))

  val schema = new StructType()
    .add("prop",new StructType()

  val df = spark.createDataFrame(spark.sparkContext.parallelize(data),schema)

  val columnNameToCheck="name"
    println("column exists")
    println("column not exists")
  //column exists





Refer to Spark GitHub Examples for more example on Spark


In this Spark article, you have learned how to check if the column exists/present in DataFrame columns, if it present in nested struct columns, by case insensitive and finally check if it exists with the same Data type.

Happy Learning !!


Naveen (NNK) is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Spark Check Column Present in DataFrame