获取数组列的大小/长度 [英] Get the size/length of an array column

查看:29
本文介绍了获取数组列的大小/长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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>]

推荐答案

您可以使用 size 函数:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆