擅长:python、mysql、java
<p>谢谢你的快速反应。我的代码比我发布的代码要复杂一点,而且我实际上是在匹配字符而不是数字。我修改了jerzael对此的响应,脚本功能正确。以下是我使用的测试脚本:</p>
<pre><code>import pandas as pd
import numpy as np
import re
dct= {'love (one)()':'john',
'smith (two)()':'doe',
'ken (three)()':'yearns'}
df = pd.DataFrame({'MAN': ['test|smith (two)()end','test|love (one)()end','test|ken (three)()end'],
'MONTH': ['Jan','Feb','Mar'],
'DD': ['1','12','22']})
for init, repl in dct.items():
i = re.findall(r'\w+', init)[0]
df.loc[df['MAN'].str.contains(i),'MAN'] = repl
print(df)
</code></pre>
<p>对于像我这样的初学者来说,正则表达式how to documentation是必须的(<a href="https://docs.python.org/3/howto/regex.html#regex-howto" rel="nofollow noreferrer">https://docs.python.org/3/howto/regex.html#regex-howto</a>)</p>
<p>干杯</p>