抱歉,不太熟悉用单独的框发布问题。
我在项目的一个特定阶段遇到了这个问题。复制以下内容。
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 say
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
您可能使用旧版本的 Pandas,其中默认参数是
regex=True
((1)
是常规模式)。将 regex=False
改为 .str.replace
:
print(df.FailCodes.str.replace("(1)", "p1q", regex=False))
打印:
0 4301,4090,5003p1q
Name: FailCodes, dtype: object