python - Pandas Dataframe Replace 不适用于子句
问题描述
我有一个带有 A 列的数据框,其中包含以下形式的值:
Col A
this is to be replaced
nonsense, this is to be replaced
nonsense
garbage
this is to be replace, nonsense
理想输出:
Col A
this has been replaced
nonsense, this has been replaced
nonsense
garbage
this has been replaced, nonsense
我试过了:
df['Col A'].replace('this is to be replaced', 'this has been replaced')
df['Col A'].str.replace('this is to be replaced', 'this has been replaced', regex=True, inplace=True)
df['Col A'].replace({'this is to be replaced':'this has been replaced'}, regex=True, inplace=True)
df['Col A'].replace(regex= ['this is to be replaced'], value= 'this has been replaced')
基本上所有解决这个问题的标准方法。问题似乎是子字符串中的空格。当我尝试替换特定单词时,它可以正常工作。
有任何想法吗?
编辑:我尝试了你们所有的建议,但它们不起作用。作为附加上下文:
要替换的确切字符串是:
MATHEMATICS (Math 1601 & 1602)
到
MATHEMATICS (Math 1601 & Math 1602)
我也试过:
df['col A'] = df['col A'].replace('1602', 'Math 1602')
解决方案
推荐阅读
- c - 从 Linux 编译到 Windows
- spring - Global exception handler across the camel context – covering all routes
- swift - 在 UILabel 中查看带两位小数的数字
- javascript - 从 AJAX 函数调用 Django 视图
- react-native - Cant Build Apk from react native in ubutnu
- jekyll - 不要在 Jekyll 构建中输出 markdown,只输出 HTML
- android - 我们可以让 jetifier 使用生成的代码吗
- javascript - 页面第二次呈现后 Jquery 不起作用
- c# - 该软件(Microsoft Visual Studio)无法调试
- jquery - bootstrap4 的下拉列表不起作用