Reputation: 202
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?
Upvotes: 0
Views: 9205
Reputation: 10703
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
Upvotes: 8