我是PySpark的新手,我现在面临着以下问题的挑战。
我有一个火花df
如下
DeviceID max(A) max(B) max(INUT)
0023002 2.5 3.7 8.1
0023045 2.2 1.3 11.3
0023008 4.7 2.3 1.9
如何添加另一列作为“Status”,其中的值将基于以下逻辑。你知道吗
if 0.20 * max(INUT) > max(max(A),max(B)) then Status = 'Imbalance' else 'Balance'
上述逻辑预期将产生以下数据帧。你知道吗
DeviceID max(A) max(B) max(INUT) Status
0023002 2.5 3.7 8.1 'Balance'
0023045 2.2 1.3 11.3 'ImBalance'
0023008 4.7 2.3 1.9 'Balance'
现在要实现上面的df
,下面是我正在使用的代码
from pyspark.sql.function import col
import pyspark.sql.function as F
df_final = df.withColumn(
'Status',
F.when(col('max(INUT)')*0.20 > F.greatest(col('max(A)'),col('max(B)'),
'Imbalance')\
.otherwise('Balance')
上面的代码段引发了一个错误
AttributeError: 'tuple' object has no attribute 'otherwise'
我错过了什么?如有任何提示,将不胜感激。你知道吗
有一些小的语法错误,这是您的最终代码:
以及一些评论:
pyspark.sql.functions
中的函数,只需使用F alias。你不需要导入两次。你知道吗max(A) -> max_A
,因为我相信它更容易阅读输出:
相关问题 更多 >
编程相关推荐