如何从pyspark.sql.function中提取值? [英] How to extract value from pyspark.sql.function?

查看:93
本文介绍了如何从pyspark.sql.function中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些pyspark.sql.functions

I'm using some pyspark.sql.functions:

print(ratings.select(mean('rating')).take(1))
print(ratings.select(stddev('rating')).take(1))

输出为:

[Row(avg(rating)=3.581564453029317)]
[Row(stddev_samp(rating,0,0)=1.1171018453732544)]

如何提取值,以便可以将其分配给变量,例如

How can I extract the value so that I can assign it to a variable, e.g.

mean_ratings = ratings.select(mean('rating'))

推荐答案

Take返回行列表.索引到列表中以获取第一行,然后拉出您要查找的字段:

Take returns a list of Rows. Index into the list to get the first row, then pull out the field you are looking for:

mean_ratings = ratings.select(mean('rating')).take(1)[0]['avg(rating)']

这篇关于如何从pyspark.sql.function中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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