我正在尝试使用以下代码在熊猫数据框中进行替换:
replacerscompanya = {',':'','.':'','-':'','ltd':'limited','&':'and'}
df1['CompanyA'] = df1['CompanyA'].replace(replacerscompanya)
replacersaddress1a = {',':'','.':'','-':'','ltd':'limited','&':'and', r'\brd\b':'road'}
df1['Address1A'] = df1['Address1A'].replace(replacersaddress1a)
replacersaddress2a = {',':'','.':'','-':'','ltd':'limited','&':'and', r'\brd\b':'road'}
df1['Address2A'] = df1['Address2A'].replace(replacersaddress2a)
它没有给我一个错误,但是当我检查数据框时,没有任何替换。
我以前只是使用下面的几行代码来达到相同的结果,但我希望创建一些更简单的调整方法。
df1['CompanyA'] = df1['CompanyA'].str.replace('.','')
关于这里发生的事情有什么想法吗?
谢谢!
.
在字典中转义,因为特殊的正则表达式字符并添加了regex=True
用于替换子字符串以及用正则表达式替换的参数:
replacersaddress1a = {',':'','\.':'','-':'','ltd':'limited','&':'and', r'\brd\b':'road'}
df1['CompanyA'] = df1['CompanyA'].replace(replacerscompanya, regex=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句