你好吗?
所以,我需要将一些用pandas编写的代码转换成PySpark,但是到目前为止我还是遇到了一些问题,我已经尝试使用PySpark的函数when
,但是结果是错误的,你能帮忙吗?我?这很简单,我从 PySpark 开始
df = df.assign(new_col=np.where(df.col1.isnull()
,np.where(df.col2.isnull(), df.col3,
np.where(df.col3.isnull(), df.col2,((df.col2+df.col3/2))
),df.col1)))
这里的主要目标是了解如何使用嵌套的 when
。
我先谢谢你!!
答案 0 :(得分:1)
如果没有示例输入输出,很难知道会发生什么,特别是考虑到您的代码也不能在 Pandas 中运行。多个 when 语句应该替换嵌套的 np.where 语句。
下面的代码
from pyspark.sql.functions import *
import pyspark.sql.functions as F
df.withColumn('newcol1', when(col('col1').isNull()&col('col2').isNull(), col('col3'))).withColumn('newcol1', when(col('col3').isNull()&col('col1').isNotNull(), col('col2')).otherwise(col('col3'))).withColumn('newcol1', when(col('col3').isNull()&col('col2').isNull(), col('col1')).otherwise(col('newcol1'))).show()