5

The below link explains how to implement IF ELSE in Spark.

How do I use multiple conditions with pyspark.sql.funtions.when()?

If my spark dataframe looks like this

A  B  C
10 2 300
20 3 200
30 7 500
20 1 700

But how do I implement the below logic which contains ELSE IF in spark.

If A>=20:
    flag = 1
elif B> 4
    flag = 2
elif C>500
    flag = 3
elif (1<A<20 & B<2)
    flag = 4
elif (1<A<20 & C<300)
    flag = 5
elif (100<C<400 & B>6)
    flag = 6
else flag = 0

Note:

  • I want a solution directly on Spark dataframe without operation on RDD or UDF.
  • I use chain of when otherwise for normal IF else condition but its difficult to implement ElSE IF with all conditions when there are lot of conditions.
GeorgeOfTheRF
  • 8,244
  • 23
  • 57
  • 80

0 Answers0