.pandas.series 的 str.replace 方法无法按预期工作

问题描述 投票:0回答:1

抱歉,不太熟悉用单独的框发布问题。

我在项目的一个特定阶段遇到了这个问题。复制以下内容。

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

python-3.x pandas dataframe
1个回答
0
投票

您可能使用旧版本的 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
© www.soinside.com 2019 - 2024. All rights reserved.