問題描述
在 Scala 中,我可以使用 get(#)
或 getAs[Type](#)
從數(shù)據(jù)幀中獲取值.在 pyspark
中應(yīng)該怎么做?
In Scala I can do get(#)
or getAs[Type](#)
to get values out of a dataframe. How should I do it in pyspark
?
我有一個兩列 DataFrame:item(string)
和 salesNum(integers)
.我做了一個 groupby
和 mean
來獲得這些數(shù)字的平均值,如下所示:
I have a two columns DataFrame: item(string)
and salesNum(integers)
. I do a groupby
and mean
to get a mean of those numbers like this:
saleDF.groupBy("salesNum").mean()).collect()
它有效.現(xiàn)在我在一個數(shù)據(jù)框中有了一個平均值.
and it works. Now I have the mean in a dataframe with one value.
如何從數(shù)據(jù)框中獲取該值以獲取浮點數(shù)的平均值?
How can I get that value out of the dataframe to get the mean as a float number?
推薦答案
collect()
將結(jié)果作為 python 列表返回.要從列表中取出值,您只需要像這樣獲取第一個元素:
collect()
returns your results as a python list. To get the value out of the list you just need to take the first element like this:
saleDF.groupBy("salesNum").mean()).collect()[0]
這篇關(guān)于從數(shù)據(jù)框中獲取價值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!