Answers for "alter table add column spark sql example"

1

add column in spark dataframe

from pyspark.sql.functions import lit

df = sqlContext.createDataFrame(
    [(1, "a", 23.0), (3, "B", -23.0)], ("x1", "x2", "x3"))

df_with_x4 = df.withColumn("x4", lit(0))
df_with_x4.show()

## +---+---+-----+---+
## | x1| x2|   x3| x4|
## +---+---+-----+---+
## |  1|  a| 23.0|  0|
## |  3|  B|-23.0|  0|
## +---+---+-----+---+
Posted by: Guest on July-10-2020
0

how to add a column to a dataframe in scala spark permanent

val df2 = df.select(col("EmpId"),col("Salary"),lit("1").as("lit_value1"))
df2.show()
Posted by: Guest on March-30-2021

Code answers related to "alter table add column spark sql example"

Python Answers by Framework

Browse Popular Code Answers by Language