简体   繁体   中英

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

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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