[英]Find and replace substrings in a Pandas dataframe ignore case
df.replace('Number', 'NewWord', regex=True)
如何用 NewWord 替換Number
或number
或NUMBER
與使用標准正則表達式相同,使用i
標志。
df = df.replace('(?i)Number', 'NewWord', regex=True)
當然, df.replace
在標志必須作為正則表達式字符串(而不是標志)的一部分傳遞的意義上是有限制的。 如果這是使用str.replace
,您可以使用case=False
或flags=re.IGNORECASE
。
只需在str.replace
使用case=False
。
例子:
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
[編輯] :如果您要在多個列中查找子字符串,則可以選擇具有object
dtypes 的所有列,並將上述解決方案應用於它們。 例子:
>>> 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
野蠻。 這僅在整個字符串是'Number'
或'NUMBER'
時才有效。 它不會替換較大字符串中的那些。 當然,僅限於這兩個詞。
df.replace(['Number', 'NUMBER'], 'NewWord')
更多蠻力
如果不夠明顯,這遠不如@coldspeed 的回答
import re
df.applymap(lambda x: re.sub('number', 'NewWord', x, flags=re.IGNORECASE))
或者從@coldspeed 的回答中得到提示
df.applymap(lambda x: re.sub('(?i)number', 'NewWord', x))
如果您要轉換的文本位於數據框的特定列中,則此解決方案將起作用:
df['COL_n'] = df['COL_n'].str.lower()
df['COL_n'] = df['COL_n'].replace('number', 'NewWord', regex=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.