Applying when condition only when column exists in the dataframe

后端 未结 1 662
无人及你
无人及你 2020-12-07 06:15

I am using spark-sql-2.4.1v with java8. I have a scenario where I need to perform certain operation if columns presents in the given dataframe column list

I have Samp

1条回答
  •  春和景丽
    2020-12-07 06:52

    You can create a column having all the column names. then you can check if the column is present or not and process if it is available-

     df.withColumn("columns_available", array(df.columns.map(lit): _*))
          .withColumn("column1_org",
          when( array_contains(col("columns_available"),"column1") , col("column1")))
          .withColumn("x",
            when( array_contains(col("columns_available"),"column4") , col("column1")))
          .withColumn("column2_new",
            when( array_contains(col("columns_available"),"column2") , sqrt("column2")))
          .show(false)
    

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