标题翻译
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|
+---+-----+
专注分享java语言的经验与见解,让所有开发者获益!
评论