英文:
How to split a column into a list and save it into a new .csv file
问题
我有一个数据框,有两列:学生ID和他们的课程。课程列有多个由";"分隔的值。如何将课程拆分为列表,并将每对(学生ID、课程1)、(学生ID、课程2)保存到一个新的CSV文件中?
英文:
I have a data frame with two columns: student ID and their courses. The course column has multiple values separated by ";". How can I split genres into a list and save every pair (studentID, genre1), (studetID, genre2) into a new CSV file?
答案1
得分: 1
你可以尝试使用 split
和 explode
函数:
val df = Seq((1, "a;b;c")).toDF("id", "values")
df.show()
val df2 = df.select($"id", explode(split($"values", ";")).as("value"))
df2.show()
df2.write.option("header", "true").csv("/path/to/csv");
+---+------+
| id|values|
+---+------+
| 1| a;b;c|
+---+------+
+---+-----+
| id|value|
+---+-----+
| 1| a|
| 1| b|
| 1| c|
+---+-----+
英文:
You could try split
and explode
:
val df = Seq((1,("a;b;c"))).toDF("id","values")
df.show()
val df2 = df.select($"id", explode(split($"values",";")).as("value"))
df2.show()
df2.write.option("header", "true").csv("/path/to/csv");
+---+------+
| id|values|
+---+------+
| 1| a;b;c|
+---+------+
+---+-----+
| id|value|
+---+-----+
| 1| a|
| 1| b|
| 1| c|
+---+-----+
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论