-
四季花海
您可以将正则表达式替换为负lookahead:#no idea why Inc|LLC or LLC|Inc will skip the firstdf['Column_Name'].str.replace(', (?!=|Inc|LLC)', '_')输出:0 TEXAS ENERGY MUTUAL, LLC_BOBBY GILLIAM_STEVE P...1 Grape, LLC_Andrea Gray_Jack Smith2 Stephen Winters_Apple_pear, Inc_Sarah SmithName: ColumnName, dtype: object
-
炎炎设计
使用 python 正则表达式模块re for 与模式, (?!Inc|LLC)查找所有出现的 , 不带以下Inc或LLCimport restrings = ["Banana, orange", "Grape, LLC", "Apple, pear, Inc"][re.sub(", (?!Inc|LLC)",'_',string) for string in strings]#['Banana_orange', 'Grape, LLC', 'Apple_pear, Inc']
-
LEATH
简单的方法: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')