2017-09-27 187 views
0

我是pyspark的新手,正在開發我的第一個火花項目,我面臨兩個問題。替換pyspark數據框中的值

一)無法使用

df["col1"].show() 

***TypeError: 'Column' object is not callable*** 

二)無法與聚合值來取代值在我的火花像數據幀平均

Code: 
from pyspark import SparkConf, SparkContext 
from pyspark.sql.functions import * 
from pyspark.sql import Row, HiveContext, SQLContext, Column 
from pyspark.sql.types import * 

df = hive_context.table("db_new.temp_table") 
df.select("col1").fillna(df.select("col1").mean()) 

***AttributeError: 'DataFrame' object has no attribute 'mean'*** 

任何幫助是極大的讚賞引用列!

更新:

我嘗試下面的代碼片段,但它返回另一個錯誤。

df.withColumn("new_Col", when("ColA".isNull,df.select(mean("ColA")) 
    .first()(0).asInstanceOf[Double]) 
    .otherwise("ColA")) 

AttributeError: 'str' object has no attribute 'isNull' 

回答

-2

這應該工作:

df[["col1"]].show() 
+0

謝謝!這解決了我的第一個問題。任何建議在第二個? – pysparker

+0

對不起,沒有看到第二位,我想第二部分回答了你的問題的意見 – ags29