scala 获取数组列的大小/长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46098573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:25:51 来源:igfitidea点击:
Get the size/length of an array column
提问by alukard990
I'm new in Scala programming and this is my question: How to count the number of string for each row? My Dataframe is composed of a single column of Array[String] type.
我是 Scala 编程的新手,这是我的问题:如何计算每行的字符串数?我的数据框由 Array[String] 类型的单列组成。
friendsDF: org.apache.spark.sql.DataFrame = [friends: array<string>]
回答by Psidom
You can use the sizefunction:
您可以使用该size功能:
val df = Seq((Array("a","b","c"), 2), (Array("a"), 4)).toDF("friends", "id")
// df: org.apache.spark.sql.DataFrame = [friends: array<string>, id: int]
df.select(size($"friends").as("no_of_friends")).show
+-------------+
|no_of_friends|
+-------------+
| 3|
| 1|
+-------------+
To add as a new column:
要添加为新列:
df.withColumn("no_of_friends", size($"friends")).show
+---------+---+-------------+
| friends| id|no_of_friends|
+---------+---+-------------+
|[a, b, c]| 2| 3|
| [a]| 4| 1|
+---------+---+-------------+

