我正在使用一些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'))
Take返回一个行列表。索引到列表中以获得第一行,然后拉出您正在查找的字段:
mean_ratings = ratings.select(mean('rating')).take(1)[0]['avg(rating)']