3 回答
TA贡献1811条经验 获得超5个赞
您可以将正则表达式替换为负lookahead:
#no idea why Inc|LLC or LLC|Inc will skip the first
df['Column_Name'].str.replace(', (?!=|Inc|LLC)', '_')
输出:
0 TEXAS ENERGY MUTUAL, LLC_BOBBY GILLIAM_STEVE P...
1 Grape, LLC_Andrea Gray_Jack Smith
2 Stephen Winters_Apple_pear, Inc_Sarah Smith
Name: ColumnName, dtype: object
TA贡献1808条经验 获得超4个赞
使用 python 正则表达式模块re for 与模式, (?!Inc|LLC)
查找所有出现的 ,
不带以下Inc
或LLC
import re
strings = ["Banana, orange", "Grape, LLC", "Apple, pear, Inc"]
[re.sub(", (?!Inc|LLC)",'_',string) for string in strings]
#['Banana_orange', 'Grape, LLC', 'Apple_pear, Inc']
TA贡献1936条经验 获得超6个赞
简单的方法:
def replace(str):
x = str.split(', ')
buf = x[0]
for i in range(1, len(x)):
if x[i].startswith('LLC'):
buf += ', ' + x[i]
elif x[i].startswith('Inc'):
buf += ', ' + x[i]
else:
buf += '_' + x[i]
return buf
然后尝试replace('a, b, LLC, d')
添加回答
举报