3 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以將正則表達(dá)式替換為負(fù)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貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用 python 正則表達(dá)式模塊re?for 與模式, (?!Inc|LLC)
查找所有出現(xiàn)的?,
不帶以下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貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
簡(jiǎn)單的方法:
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')
添加回答
舉報(bào)