2017-08-05 30 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