I want add a new column in my existing dataframe. Below is my dataframe -
+---+---+-----+
| x1| x2| x3|
+---+---+-----+
| 1| a| 23.0|
| 3| B|-23.0|
+---+---+-----+
I am able to add df = df.withColumn("x4", lit(0))
like this
+---+---+-----+---+
| x1| x2| x3| x4|
+---+---+-----+---+
| 1| a| 23.0| 0|
| 3| B|-23.0| 0|
+---+---+-----+---+
but I want to add a array list to my df.
Supose this [0,0,0,0]
is my array to add and after adding my df will look like this -
+---+---+-----+---------+
| x1| x2| x3| x4|
+---+---+-----+---------+
| 1| a| 23.0|[0,0,0,0]|
| 3| B|-23.0|[0,0,0,0]|
+---+---+-----+---------+
I tried like this -
array_list = [0,0,0,0]
df = df.withColumn("x4", lit(array_list))
But it is giving error
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.sql.functions.lit.
: java.lang.RuntimeException: Unsupported literal type class java.util.ArrayList [0, 0, 0, 0, 0, 0]
Do anybody know how to do this?