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"))
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With