How to create an empty dataframe in Scala? Last Updated : 29 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to create an empty dataframe in Scala. We can create an empty dataframe in Scala by using the createDataFrame method provided by the SparkSession object. Syntax to create an empty DataFrame: val df = spark.emptyDataFrame Example of How to create an empty dataframe in Scala: Scala import org.apache.spark.sql.{SparkSession, DataFrame} import org.apache.spark.sql.types.{StructType, StructField, StringType} // Create SparkSession val spark = SparkSession.builder() .appName("EmptyDataFrameExample") .getOrCreate() // Define schema for the empty DataFrame val schema = new StructType(Array( StructField("column_name", StringType, true) )) // Create an empty DataFrame using createDataFrame // method with an empty RDD and the schema val emptyDF: DataFrame = spark.createDataFrame(spark.sparkContext.emptyRDD[Row], schema) // Show the schema of the empty DataFrame emptyDF.printSchema() Output: In this output we can see that empty dataframe is created in scalaExplanation of the above example:Import necessary classes from the org.apache.spark.sql package, including SparkSession, DataFrame, StructType, StructField, and StringType.Create a SparkSession object named spark.Define a schema for the empty DataFrame. In this example, we're creating a DataFrame with a single column named "column_name" of type StringType. You can define your schema according to your requirements.Use the createDataFrame method of the SparkSession object (spark) to create an empty DataFrame. Pass an empty RDD of type Row and the schema you defined earlier.The resulting DataFrame (emptyDF) will have the schema defined earlier and no rows.Print the schema of the empty DataFrame using the printSchema method. Comment More info K kokaneit92 Follow Improve Article Tags : Scala Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like