Q: Using regex for same root but different suffix and prefix I'd like to replace all the cells in my dataframe where the root RANG appears.
It doesn't matter the suffix and the prefix, also the root could sometimes appear in lower case. I want to replace all of that with np.NaN
aa#RANG       | Match
aa#RANGbd     | Match
#RANG         | Match
'01.02'       | No Match
rang         | Match 

I tried the following:
df = df.replace(to_replace=r'(.*)RANG(.*)', value=np.NaN, regex=True)

This is excluding suffix and suffixes but not dealing with lower case in the same time. How do I remove lower case in just one run, instead of doing another line of code?

A: You can try stemming the words first using nltk if you use real words. This will reduce the words to their stem more reliably than using a regular expression for this:
import nltk

from nltk.stem.porter import *

stemmer = PorterStemmer()

stemmer.stem("runner") 
# --> run

If you use non-existing words like aa#RANGbd and you want both lower and upper case to be matched, you can create a regex pattern that ignores case:
import re
pattern = re.compile('rang', flags=re.IGNORECASE)

df = df.replace(to_replace=pattern, value=np.NaN, regex=True)


A: Please use df.replace
Data
df=pd.DataFrame({'text': ['william RANG', 'he_RANG'], 'T':['WEWE_RANG', 'KLLL_RANG___KLOP_RANG']})

   text                      T
0  william RANG              WEWE_RANG
1       he_RANG  KLLL_RANG___KLOP_RANG



df=df.replace(to_replace=r'RANG', value='NaN', regex=True)
print(df)
  

      text                    T
0  william NaN             WEWE_NaN
1       he_NaN  KLLL_NaN___KLOP_NaN

