Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace substrings in a Pandas dataframe ignore case

df.replace('Number', 'NewWord', regex=True)

how to replace Number or number or NUMBER with NewWord

like image 781
Pete Brush Avatar asked Aug 15 '18 18:08

Pete Brush


2 Answers

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.

like image 150
cs95 Avatar answered Nov 14 '22 22:11

cs95


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
like image 38
sacuL Avatar answered Nov 15 '22 00:11

sacuL