从pyspark中的数据框访问计数值

时间:2017-08-05 18:18:31

标签: python-3.x pyspark

我希望你能帮上忙。

我有这个数据框,我想选择,例如,预测的计数== 4

Code: 
the_counts=df.select('prediction').groupby('prediction').count()
the_counts.show()


+----------+-----+
|prediction|count|
+----------+-----+
|         1|    8|
|         6|   14|
|         5|    5|
|         4|    8|
|         8|    5|
|         0|    6|
+----------+-----+

所以,我可以将该值赋给变量。因为这将在一个循环中进行多次迭代。

我管理了这个,但它是通过创建一个不同的数据帧,然后将该datafram更改为一个数字。

dfva = the_counts.select('count').filter(the_counts.prediction ==6)
dfva.show()


+-----+
|count|
+-----+
|   14|
+-----+

有没有办法在没有这么多步骤或最有效的方式的情况下直接访问号码?

这是python 3.x和spark 2.1

非常感谢

1 个答案:

答案 0 :(得分:2)

你可以通过first()方法直接获取值,

>>> dfva = the_counts.filter(the_counts['prediction'] == 6).first()['count']
>>> type(dfva)
<type 'int'>
>>> print(dfva)
14