window 函数(滞后)实现和 IsNotIn 在 pyspark 中的使用

window functions( lag) implementation and the use of IsNotIn in pyspark

下面附上T-SQL代码。我尝试使用 window 函数将其转换为 pyspark,该函数也已附加。

  case 
             when eventaction = 'OUT' and lag(eventaction,1) over (PARTITION BY barcode order by barcode,eventdate,transactionid) <> 'IN'  
                  then 'TYPE4'
             else ''
      end as TYPE_FLAG,

Pyspark 代码使用 window 函数给出错误 lag

Tgt_df = Tgt_df.withColumn(
    'TYPE_FLAG',
    F.when(
        (F.col('eventaction')=='OUT')
        &(F.lag('eventaction',1).over(w).isNotIn(['IN'])),
    "TYPE4"
).otherwise(''))  

但它不起作用。怎么办!?

它给你一个错误,因为列对象没有 isNotIn 方法。 如果您只是发布错误消息,那将是显而易见的...

而是使用 ~(非)运算符。

&( ~ F.lag('eventaction',1).over(w).isin(['IN'])),

可用方法列表在 official documentation 中。