繁体   English   中英

基于前一列的Spark Df Check Column值

[英]Spark Df Check Column value based on the previous column

嗨,我一直坚持在Spark DF上实现自定义条件。 基本上我想基于列中存在的Null值将列标记为0或1,即如果有的话

列包含null,对应于该行的状态将为0,否则为1

 val someData = Seq(
    Row(8, "bat"),
    Row(64, "mouse"),
    Row(null, "rat")
  )

  val someSchema = List(
    StructField("number", IntegerType, true),
    StructField("word", StringType, true)
  )

  val someDF = sparkSession.createDataFrame(
    sparkSession.sparkContext.parallelize(someData),
    StructType(someSchema)
  )
val fieldList: Seq[Column] = Seq(col("word"),col("number"))


 val df = fieldList.foldLeft(inputDf)(
      (inputDf, f) => {
       dfin = inputDf.withColumn(Status, lit(0))
        dfin
          .withColumn(
            Status,
            when(f.isNotNull and col("status").isin(0), 0).otherwise(1)
          )

      }

但它基于fieldList的最后一列进行检查,但它应该像

col 1  col2  status
zyx .  pqe .  0
null . zyz . 1
xdc . null  1
null  null  1
val df = someDF.withColumn("status", when(fieldList.map(x => col(x).isNull).reduce(_ || _), 1).otherwise(0)

这个想法是首先将每个列的名称转换为一列,并检查它是否为null(地图),现在,如果至少一个为null,则简单的reduce会导致true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM