df.replace('Number', 'NewWord', regex=True)
how to replace Number
or number
or NUMBER
with NewWord
Same as you'd do with the standard regex, using the i
flag.
df = df.replace('(?i)Number', 'NewWord', regex=True)
Granted, df.replace
is limiting in the sense that flags must be passed as part of the regex string (rather than flags). If this was using str.replace
, you could've used case=False
or flags=re.IGNORECASE
.
Simply use case=False
in str.replace
.
Example:
df = pd.DataFrame({'col':['this is a Number', 'and another NuMBer', 'number']})
>>> df
col
0 this is a Number
1 and another NuMBer
2 number
df['col'] = df['col'].str.replace('Number', 'NewWord', case=False)
>>> df
col
0 this is a NewWord
1 and another NewWord
2 NewWord
[Edit]: In the case of having multiple columns you are looking for your substring in, you can select all columns with object
dtypes, and apply the above solution to them. Example:
>>> df
col col2 col3
0 this is a Number numbernumbernumber 1
1 and another NuMBer x 2
2 number y 3
str_columns = df.select_dtypes('object').columns
df[str_columns] = (df[str_columns]
.apply(lambda x: x.str.replace('Number', 'NewWord', case=False)))
>>> df
col col2 col3
0 this is a NewWord NewWordNewWordNewWord 1
1 and another NewWord x 2
2 NewWord y 3
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