从字符串中删除点符号 - 熊猫数据帧



我正在尝试将'.符号替换为'':

excel_data_df['serialNumber'] = df2[['Serial number', 'Serial number.1']].agg(''.join, axis=1).replace(to_replace = '.', value = '', regex = True)

我的字符串:"TF013168。 名称:序列号,dtype:对象,在Excel中保存为文本的数字。

但结果我从字符串中删除了所有字符。 还有其他方法可以做到吗?

提前谢谢。

转义..,因为.是替换子字符串的特殊正则表达式字符:

excel_data_df['serialNumber'] = df2[['Serial number', 'Serial number.1']].agg(''.join, axis=1).replace(to_replace = '.', value = '', regex = True)

使用str.replaceregex=False选项以防止将.解释为"任何字符"正则表达式。在您的情况下,无需使用正则表达式引擎。KBK无论如何,regex=True默认值计划在未来更改为regex=False

excel_data_df['serialNumber'] = (df2[['Serial number', 'Serial number.1']]
.agg(''.join, axis=1)
.replace(to_replace='.', value='', regex=False)

最新更新