简体   繁体   中英

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

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

Brutish. This only works if the whole string is either 'Number' or 'NUMBER' . It will not replace those within a larger string. And of course, it is limited to just those two words.

df.replace(['Number', 'NUMBER'], 'NewWord')

More Brute Force
If it wasn't obvious enough, this is far inferior to @coldspeed's answer

import re

df.applymap(lambda x: re.sub('number', 'NewWord', x, flags=re.IGNORECASE))

Or with a cue from @coldspeed's answer

df.applymap(lambda x: re.sub('(?i)number', 'NewWord', x))

This solution will work if the text you're looking to convert is in specific columns of your dataframe:

    df['COL_n'] = df['COL_n'].str.lower() 
    df['COL_n'] = df['COL_n'].replace('number', 'NewWord', regex=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM