2

I want to explode Array[(Int, Int)] column from a dataframe

INPUT:

colA newCol
1     [[1a, 2],[3c, 5u]]
2     [[1c, 9m], [5e, 7l]]

OUTPUT:

colA newCol
1     1a
1     3c
2     1c
2     5e
1
  • 1
    Please use text instead of images. You should also add what have you tried and the error, if you encountered any. For more information, read How to Ask. Commented Aug 1, 2019 at 6:40

1 Answer 1

2

Here is my approach.

+----+--------------------+
|col1|col2                |
+----+--------------------+
|1   |[[1a, 2b], [3c, 5u]]|
|2   |[[1c, 9m], [5e, 7l]]|
+----+--------------------+

This is your dataframe and

df.withColumn("t", explode($"col2")).selectExpr("col1", "t[0]").show

my code results to

+----+----+
|col1|t[0]|
+----+----+
|   1|  1a|
|   1|  3c|
|   2|  1c|
|   2|  5e|
+----+----+
Sign up to request clarification or add additional context in comments.

3 Comments

How can we solve it if t[0] column is of IntegerType
Let me give your desired dataframe.
Was trying something like this: val res = df.withColumn("tup", explode($"newCol")).select("colA", "tup") res.select(col("colA"), col("tup")("value").as("uId"))

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.