从数据框中获取值 [英] get value out of dataframe
问题描述
在 Scala 中,我可以执行 get(#)
或 getAs[Type](#)
从数据帧中获取值.我应该如何在 pyspark
中做到这一点?
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
来得到这些数字的平均值:
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()
它的工作原理.现在我在一个数据框中获得了一个值的平均值.
and it works. Now I have the mean in a dataframe with one value.
如何从数据框中获取该值以将平均值作为浮点数?
How can I get that value out of the dataframe to get the mean as a float number?
推荐答案
collect()
将结果作为 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]
这篇关于从数据框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!