How to conditionally replace value in a column based on evaluation of expression based on another column in Pyspark?

前端 未结 1 1908
借酒劲吻你
借酒劲吻你 2021-01-31 03:18
import numpy as np

df = spark.createDataFrame(
    [(1, 1, None),
     (1, 2, float(5)),
     (1, 3, np.nan),
     (1, 4, None),
         


        
1条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 04:09

    You should be using the when (with otherwise) function:

    from pyspark.sql.functions import when
    
    targetDf = df.withColumn("timestamp1", \
                  when(df["session"] == 0, 999).otherwise(df["timestamp1"]))
    

    0 讨论(0)
提交回复
热议问题