如何将一列拆分为列表并将其保存到新的 .csv 文件中。

huangapple go评论67阅读模式
英文:

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

你可以尝试使用 splitexplode 函数:

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|
+---+-----+

huangapple
  • 本文由 发表于 2020年3月16日 21:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定