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

How to extract value from pyspark.sql.function?

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

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

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