代码之家  ›  专栏  ›  技术社区  ›  Josh

如何有条件地用整数替换空值Pyspark数据帧

  •  -1
  • Josh  · 技术社区  · 7 年前

    我正在努力(在pyspark/spark内)。具体而言,数据帧如下所示:

    ID    response_variable    id_name
    id_1   NULL                 "randomidDKHFD"
    id_2   NULL                 NULL
    id_3   NULL                 "randomid86438"
    

    我希望能够转换数据帧,以便无论何时id_name有一个非空条目,response_变量都变为1。因此,在本例中:

    ID    response_variable    in_data
    id_1   1                 "randomidDKHFD"
    id_2   NULL                NULL
    id_3   1                 "randomid86438"
    

    有人知道怎么做吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sailesh Kotha    7 年前

    你可以用 withColumn when

    蟒蛇:

    yourDf.withColumn("response_variable", when(col("id_name").isNotNull(), lit(1)).otherwise(col("response_variable")))
    

    yourDf.withColumn("response_variable", when(col("id_name").isNotNull, lit(1)).otherwise(col("response_variable")))
    
    推荐文章