3

I have an array of list.

array([1,2,3,4.....,50])

I want to add each element in this list as a new column to my current spark dataframe. Something like this

col1|col2|new_col
-------------------
a   |aa  |1
-------------------
b   |bb  |2
-------------------
c   |cc  |3
-------------------
d   |dd  |4
-------------------
...
...
...
...
-------------------
z  |zz  |50
--------------------

withColumn seems to not work for something like this.

0

1 Answer 1

3

Use arrays_zip function, for this first we need to convert existing data into array & then use arrays_zip function to combine existing and new list of data. Check below code.

list_data = [1,2,3,4.....,50]

df \
.select(collect_list(struct(F.col("*"))).alias("data")) \
.withColumn("list",F.array([F.lit(i) for i in list_data])) \
.select(F.explode(F.arrays_zip(F.col("data"),F.col("list"))).alias("full_data")) \ 
.select(F.col("full_data.data.*"),F.col("full_data.list").alias("col3")) \ 
.show()

Spark Version >= 2.4.0

Sign up to request clarification or add additional context in comments.

7 Comments

getting an error 'numpy.int64' object has no attribute '_get_object_id'
Spark version??
Spark 3.0.1 Right at the withColumn statement. Array is somehow causing the problem. Used an ordinary list and it's working but array of list isn't.
Ok..I have copied that from your question and pasted .. now updated solution check once
Yea I know list is working. I don't want a list. I have an array of list. I want that as a new column. Even if I convert my array of list into a list and then use that list, It's giving out the same error. I tried with a sample list like [1,2,3], then it's working. Even if I am changing my array([list]) into a list using list(array) and then use that list. It's giving out the same error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.