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
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
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|
+----+----+
t[0] column is of IntegerTypeval res = df.withColumn("tup", explode($"newCol")).select("colA", "tup") res.select(col("colA"), col("tup")("value").as("uId"))