我有一个python函数,它返回True/False取决于数据框列的值。
def check_name(df):
if ((df.name == "ABC")):
return ((df.Value < 0.80))
return (df.Value == 0)
我将这个函数作为myFunction
传递到查询中:
def myQuery(myFunction):
df.filter(...).groupBy(...).withColumn('Result', when(myFunction(df), 0).otherwise(1))
但它失败了
Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
我觉得问题出在这个df.name == "ABC"
我试着修改为F.col('name')
==“ABC”,但我得到了同样的错误。
你能告诉我如何解决我的问题吗?
1条答案
按热度按时间jgwigjjp1#
if-else代码应该是spark中的指令(
when.otherwise
)。然后如果
myFunction
必须返回boolean
,并且您正在反转布尔值(true = 0, false = 1
),则可以将myQuery
简化为