val df = Seq(
(1, "A, B, C, D"),
(2, "E, F, G,5"),
(3, "H, I,5,7"),
(4, "J,sd,cc,23")
).toDF("number","letters")
df.show()
output:
+------+----------+
|number| letters|
+------+----------+
| 1|A, B, C, D|
| 2| E, F, G,5|
| 3| H, I,5,7|
| 4|J,sd,cc,23|
+------+----------+
val arr = df.filter($"number"===1).select($"letters").head.toSeq
val list1 = arr.toList
list1.zipWithIndex
//为什么没有列表((“a”,0),(“b”,1),(“c”,2),(“d”,3))?
output:
arr: Seq[Any] = WrappedArray(A, B, C, D)
list1: List[Any] = List(A, B, C, D)
res87: List[(Any, Int)] = List((A, B, C, D,0))
然而
List("a", "b", "c").zipWithIndex
output:
res88: List[(String, Int)] = List((a,0), (b,1), (c,2))
为什么它们都有不同输出格式的zipwithindex?
1条答案
按热度按时间ssgvzors1#
当你打电话的时候
.head
在Dataframe上返回Row
.如果你看看
list1
您将看到它只包含一个元素。您需要提取行的元素0处的字符串: