【问题标题】:Pyspark dataframe create new column from other columns and from itPyspark 数据框从其他列和从中创建新列
【发布时间】:2017-08-04 12:10:44
【问题描述】:

我有 pyspark 数据框 DF

现在我想创建一个具有以下条件的新列。

城市客户销售订单检查点

一个eee 20 20 1

b sfd 28 30 0

C 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。

请在这方面给我建议?

【问题讨论】:

    标签: pyspark pyspark-sql


    【解决方案1】:

    创建了一个专栏 df= df.withColumn("NewCol",lit(None))

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多