test =“a1-b1,a2-b2”
我希望将此字符串转换为数据框
列A和B分别保持a1,a2和b1,b2
答案 0 :(得分:3)
您可以将字符串转换为RDD,然后将其转换为DataFrame:
val s = "a1-b1,a2-b2"
val df = sc.parallelize(
s.split(",").map(_.split("-")).map{ case Array(a, b) => (a, b) }
).toDF("A", "B")
df.show
+---+---+
| A| B|
+---+---+
| a1| b1|
| a2| b2|
+---+---+