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
与使用标准正则表达式相同,使用^{} flag 。在
当然,}。在
df.replace
是有限制的,标志必须作为regex字符串(而不是标志)的一部分传递。如果这是使用str.replace
,那么您可以使用case=False
或{只需在
str.replace
中使用case=False
。在示例:
[Edit]:如果有多个列要在其中查找子字符串,则可以选择具有
^{pr2}$object
数据类型的所有列,并对其应用上述解决方案。示例:粗野的。只有当整个字符串是}时,这才有效。它不会替换较大字符串中的字符串。当然,它只限于这两个词。在
'Number'
或{更多暴力
^{pr2}$如果这还不够明显,那就远远不如@coldspeed的答案
或者是从@coldspeed的回答中得到提示
相关问题 更多 >
编程相关推荐