2017-05-29 64 views
1

我有這樣一個數據幀:當總和()的一個專欄中,我得到這個錯誤AttributeError的:「據幀」對象有沒有屬性「和」

+-----+--------+ 
|count| country| 
+-----+--------+ 
| 12| Ireland| 
| 5|Thailand| 
+-----+--------+ 

當我添加總和()函數來獲得總第一列「計數」的我得到這個錯誤:

AttributeError: 'DataFrame' object has no attribute 'sum' 

我導入from pyspark.sql.functions import sum

如何總結或我缺少什麼?

謝謝你,感謝任何幫助。

+0

請分享您的代碼 –

回答

1
>>> from pyspark.sql.functions import sum 
>>> a = [(12,"Ireland"),(5,"Thailand")] 
>>> df = spark.createDataFrame(a,["count","country"]) 
>>> df.show() 
+-----+--------+ 
|count| country| 
+-----+--------+ 
| 12| Ireland| 
| 5|Thailand| 
+-----+--------+ 

正如你可以看到here

groupBy(): Groups the DataFrame using the specified columns, so we can run aggregation on them. See GroupedData for all the available aggregate functions.

GroupedData你可以找到一組用於在數據幀聚合方法,如總和()AVG()意思是()

所以你必須在應用這些功能之前對你的數據進行分組。

>>> total = df.groupBy().sum() 
>>> total.show() 
+----------+ 
|sum(count)| 
+----------+ 
|  17| 
+----------+ 

here爲例與總和()

相關問題