Python: Replace Case Insensitive Flag Doesn't Work
In my dataframe I want to replace different ways of representing something with a single consistent string. Examples: Replace [COM, COMMERCIAL] with 'Commercial'. Replace [FALSE,
Solution 1:
Either str.replace
or replace
can be used. Just make sure the pattern matches the start (^
) and end ($
) of the string for whole cell matches.
str.replace
:
for val in valold:
dfPA[col] = dfPA[col].str.replace(rf'^{val}$', key, case=False, regex=True)
replace
:
for val in valold:
dfPA[col] = dfPA[col].replace(rf'(?i)^{val}$', key, regex=True)
*regex=False
by default for replace
so the regex
case insensitivity modifier will not work for replace without setting regex=True
as it will literally match the characters "(?i)".
Sample Data and Output:
import pandas as pd
dfPA = pd.DataFrame({
'col': ['COM', 'COMMERCIAL', 'COmMErCIaL', 'Something else',
'comical']
})
valold = ['COM', 'COMMERCIAL']
key = 'Commercial'
col = 'col'for val in valold:
dfPA[col] = dfPA[col].str.replace(rf'^{val}$', key, case=False, regex=True)
print(dfPA)
col
0 Commercial
1 Commercial
2 Commercial
3 Something else
4 comical
Post a Comment for "Python: Replace Case Insensitive Flag Doesn't Work"