0

I have below dataset

+----+-----------+
|col1|       col2|
+----+-----------+
|   1|val1, val2 |
|   2|val3, val4 |
+----+-----------+

Consider all values as String Now i want to convert it into below dataset

+----+-----------+
|col1|       col2|
+----+-----------+
|   1|val1       |
|   1|val2       |
|   2|val3       |
|   2|val4       |
+----+-----------+

How can I achieve this?

3
  • Have you tried anything? Commented Oct 5, 2020 at 8:39
  • How can we write the same for redshift. We need to create a udf for it. Can someone help me to create the udf Commented May 30, 2021 at 2:50
  • @Etisha check this answer. Commented May 31, 2021 at 7:32

1 Answer 1

8

Use split to parse comma-separated values as an array, then explode to rearrange array elements into separate rows.

df.withColumn("col2", explode(split($"col2", ","))).show
Sign up to request clarification or add additional context in comments.

1 Comment

I am using SQL editor and this worked for me SELECT explode(split(col1, ",")) FROM table

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.