简体   繁体   中英

How to add/append a new row to a DataFrame in Scala without using a SQL insert?

I have a DataFrame created in the following way.

val someDF = Seq((8, "bat"),(64, "mouse"),(-27, "horse")).toDF("number", "word")
someDF.printSchema
root
 |-- number: integer (nullable = false)
 |-- word: string (nullable = true)

Using SQL API, one can insert a row into it by creating a temp table and running an insert query. Is there any way one can append/add a new row using methods of the DataFrame API ?

You can use union :

val someDF = Seq((8, "bat"),(64, "mouse"),(-27, "horse")).toDF("number", "word")
someDF.union(Seq((10, "dog")).toDF).show
/*
+------+-----+
|number| word|
+------+-----+
|     8|  bat|
|    64|mouse|
|   -27|horse|
|    10|  dog|
+------+-----+
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM