Pyspark数据框从其他列创建新列并从中获得
问题描述:
我有pyspark数据框DFPyspark数据框从其他列创建新列并从中获得
现在我想创建一个新的列,其条件如下。
城市客户销售订单检查点
一个EEE 20 20 1
b SFD 28 30 0
ÇSSS 30 30 1
d ZZZ 35 40 0
DF = Df.withColumn("NewCol",func.when(DF.month == 1,DF.sales + DF.orders).otherwise(greatest(DF.sales,DF.orders))+ func.when(DF.checkpoint == 1,lit(0)).otherwise(func.lag("NewCol).over(Window.partitionBy(DF.city,DF.customer).orderBy(DF.city,DF.customer))))
我得到了一个错误,像NewCol没有被定义,这是预期的。
请给我推荐?
答
创建的列 DF = df.withColumn( “NEWCOL”,点亮(无))
for i in range(2):
if i<=2:
DF = Df.withColumn("NewCol",func.when(DF.month == 1,DF.sales + DF.orders).otherwise(greatest(DF.sales,DF.orders))+ func.when(DF.checkpoint == 1,lit(0)).otherwise(func.lag("NewCol).over(Window.partitionBy(DF.city,DF.customer).orderBy(DF.city,DF.customer))))</i)