2017-01-01 66 views
1

可以說給予了數據幀更新在pyspark取決於列電流值的列

+-----+-----+-----+ 
| x| y| z| 
+-----|-----+-----+ 
| 3| 5| 9| 
| 2| 4| 6| 
+-----+-----+-----+ 

我想用價值y列乘以所有的值在z列,其中z列等於6

This後顯示我的目標的解決方案,使用的代碼

from pyspark.sql import functions as F 

df = df.withColumn('z', 
    F.when(df['z']==6, df['z']*df['y']). 
    otherwise(df['z'])) 

ŧ他的問題是,df['z']df['y']被識別爲Column對象,並且鑄造它們將不起作用...

如何正確地做到這一點?

+0

如果你需要這個號碼,你會在scala中做什麼?可以說我想複製一些列表[df ['z']] index – bluesummers

+0

試試這個'df = df.withColumn('new_col',F.when(df.z == 6,(df.z * df .y))。否則(df.z))' – mrsrinivas

+0

不工作:/ – bluesummers

回答

1
from pyspark.sql import functions as F 
from pyspark.sql.types import LongType 

df = df.withColumn('new_col', 
      F.when(df.z==6, 
       (df.z.cast(LongType()) * df.y.cast(LongType())) 
      ).otherwise(df.z) 
    ) 
+0

你能否在回覆中回覆?我還有一件事要驗證 – bluesummers