我在项目的一个特定阶段遇到过这个问题.
复制以下内容:
import pandas as pd
# Recreated a sample data
data = {
"FailCodes": ['4301,4090,5003(1)'],
}
df = pd.DataFrame(data)
# Want to replace the '(1)' with 'p1q'
print(df.FailCodes.str.replace('(1)','p1q'),'\n') # Not giving expected result
# compare to string object method from standard python, which gives desired result
print('4301,4090,5003(1)'.replace('(1)','p1q'),'\n')
# Can get wanted result with following longer code,but would like explanation why first approach giving the unexpected result.
print(df.FailCodes.str.replace('(','p').str.replace(')','q' ))
- 意想不到的结果结束为:
430p1q,4090,5003(p1q)
- 预期结果:
4301,4090,5003p1q