如何从pyspark.sql.function中提取值?

时间:2016-10-12 17:23:24

标签: pyspark pyspark-sql

我正在使用一些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)]

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

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

1 个答案:

答案 0 :(得分:2)

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

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