Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace values by None if string contains character pattern?

I want to replace every string in my pandas df column departments with None if it contains a )

         departments   var1   var1.1
   1      transport     aa      uu
   2      industry)     bb      tt
   3      aviation)     cc      tt

how the dataset should look like

         departments   var1    var2
   1      transport     aa      uu
   2      None          bb      tt
   3      None          cc      tt

A similar solution is here: Replacing regex pattern with another string works, but replacing with NONE replaces all values

How can i transform it to base python as i dont use spark?

df.withColumn("departments", when(col("departments").rlike("\)"), None)
          .otherwise(col("departments"))
      )
like image 801
id345678 Avatar asked Sep 04 '26 23:09

id345678


1 Answers

With your shown samples, please try following. You could use str.contains function to find out whatever values in departments column has ) then using .loc with respect to values which we got in m variable setting None to those values.

m = df['departments'].str.contains('\)', na=False)
df.loc[m,'departments'] = None
like image 60
RavinderSingh13 Avatar answered Sep 06 '26 13:09

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!