how to place when condition dynamically with withColumn of pyspark dataframe.?

363 views Asked by At

i have pyspark dataframe like below which contain 1 columns:-

dd1=

    src
  8.8.8.8
  103.102.122.12
  192.168.9.1

I want to add column in dd1 of name "Dept" which contain name of dept ip belongs to for that i have written a regex using it will add value in dept column. But twist is i want to place condition dynamically. I have done like this

test="when(dd1.src.rlike('^192.168.9.([1-9]|1d|2[0-4])$'),'CAMERA').otherwise('Public')"
dd2=dd1.withColumn("Dept",{}).format(test)

But it is giving me error like col should be column

But when i do it by hard code like below it work fine..

 dd2=dd1.withColumn("Dept",when(dd1.src.rlike('^192.168.9.([1-9]|1d|2[0-4])$'),'CAMERA').otherwise('Public'))

Expected Output :

  src                Dept
  8.8.8.8          Public
  103.102.122.12   Public
  192.168.9.1      CAMERA

Please help me for regarding this issue..

Thanks in advance.

1

There are 1 answers

0
Sunny Shukla On

This should do the trick:

from pyspark.sql.functions import when, regexp_extract, lit
condition = regexp_extract("src", '^192.168.9.([1-9]|1d|2[0-4])$', 1)
df.select("*", when(condition=="", lit("Public")).otherwise("CAMERA").alias("Dept")).show()