从数据框中获取值 [英] get value out of dataframe

查看:35
本文介绍了从数据框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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).我做了一个 groupbymean 来得到这些数字的平均值:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆