获取数组列的大小/长度 [英] Get the size/length of an array column
本文介绍了获取数组列的大小/长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Scala编程的新手,这是我的问题:如何计算每一行的字符串数?我的数据框由Array [String]类型的单列组成.
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.
friendsDF: org.apache.spark.sql.DataFrame = [friends: array<string>]
推荐答案
You can use the size
function:
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|
+---------+---+-------------+
这篇关于获取数组列的大小/长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文