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條經驗 獲得超7個贊
簡單的方法:
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')
添加回答
舉報