用其他文本替换列中的字符串
Replace string in column with other text
这似乎是一个有很多在线示例的初级问题,但出于某种原因,它对我不起作用。
我正在尝试用值 =“基于设施的测试-OH”替换 'A' 列中值 =“Facility-based testing-OH”的所有单元格。如果您注意到,两者之间的唯一区别是单个“-”,但出于我的目的,我不想在分隔符上使用拆分功能。只是想找到需要替换的值。
我尝试了以下代码,但 none 有效。
第一种方法:
df = df.str.replace('Facility-based testing-OH','Facility based testing-OH')
第二种方法:
df['A'] = df['A'].str.replace(['Facility-based testing-OH'], "Facility based testing-OH"), inplace=True
第三种方法
df.loc[df['A'].isin(['Facility-based testing-OH'])] = 'Facility based testing-OH'
尝试:
df["A"] = df["A"].str.replace(
"Facility-based testing-OH", "Facility based testing-OH", regex=False
)
print(df)
打印:
A
0 Facility based testing-OH
1 Facility based testing-OH
df
使用:
A
0 Facility-based testing-OH
1 Facility based testing-OH
这似乎是一个有很多在线示例的初级问题,但出于某种原因,它对我不起作用。
我正在尝试用值 =“基于设施的测试-OH”替换 'A' 列中值 =“Facility-based testing-OH”的所有单元格。如果您注意到,两者之间的唯一区别是单个“-”,但出于我的目的,我不想在分隔符上使用拆分功能。只是想找到需要替换的值。
我尝试了以下代码,但 none 有效。
第一种方法:
df = df.str.replace('Facility-based testing-OH','Facility based testing-OH')
第二种方法:
df['A'] = df['A'].str.replace(['Facility-based testing-OH'], "Facility based testing-OH"), inplace=True
第三种方法
df.loc[df['A'].isin(['Facility-based testing-OH'])] = 'Facility based testing-OH'
尝试:
df["A"] = df["A"].str.replace(
"Facility-based testing-OH", "Facility based testing-OH", regex=False
)
print(df)
打印:
A
0 Facility based testing-OH
1 Facility based testing-OH
df
使用:
A
0 Facility-based testing-OH
1 Facility based testing-OH