< /ol>
Ich versuche, die Werte mit der zweiten Variation abzuzielen, und ersetzen Sie den von Kommas getrennten Substring aus einer anderen Spalte in derselben Zeile.
Code: Select all
import re
for i, x in enumerate(df['col1']):
df.loc[i,'col2'] = re.sub(r'^(func\().+,.+(\).*)', r'\1%s\2'%(x), df.loc[i,'col2'])
ersetze
Code: Select all
>>> val = 'func(y,z) and func2(a,b)'
>>> val
'func(y,z) and func2(a,b)'
>>> pattern1 = r'^(func\().+,.+(\).*)'
>>> replacement = r'\1%s\2'%('x')
>>> val2 = re.sub(pattern1, replacement, val)
>>> val2
'func(x)'
>>> pattern2 = r'^(func\().+,.+(\).+)'
>>> val2 = re.sub(pattern2, replacement, val)
>>> val2
'func(x) and func2(a,b)'
>>> val3 = 'func(y) and func2(a,b)'
>>> val4 = re.sub(pattern2, replacement, val3)
>>> val4
'func(y) and func2(a, b)'
col1 col2
0 func(a,b) and func(c,d) e
1 func(a) and func(c) b
2 func(b) and func(c,d) a
3 func(a,b,c) and func(d,e,f) g
< /code>
import re
df['col3'] = ''
for i,x in enumerate(df['col2']):
pattern = r'^(func\().+,.+(\).+)'
replacement = fr'\1{x}\2'
df.loc[i,'col3'] = re.sub(pattern,replacement,df.loc[i,'col1'])
if df.loc[i,'col3'] != df.loc[i,'col1']:
print(df.loc[i,'col1'],'--->',df.loc[i,'col3'])
>>>
func(a,b) and func(c,d) ---> func(e) and func(c,d)
func(a,b,c) and func(d,e,f) ---> func(g) and func(d,e,f)
[/code]