2017-08-05 58 views
0

我希望你不能帮忙。访问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| 
+-----+ 

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

这是蟒蛇3.x和火花2.1

非常感谢您

+0

:d你的第一行说:我希望你不能帮忙。 – ShuaiYuan

+0

明显的错误,这里的人总是可以帮助:-) – Learner

回答

2

可以先()方法采取直接的价值,

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