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: