Home > OS >  Replace string in column with other text
Replace string in column with other text

Time:09-29

This seems like an elementary question with many online examples, but for some reason it does not work for me.

I am trying to replace any cells in column 'A' that have the value = "Facility-based testing-OH" with the value = "Facility based testing-OH". If you note, the only difference between the two is a single '-', however for my purposes I do not want to use the split function on a delimeter. Simply want to locate the values that need replacement.

I have tried the following code, but none have worked.

1st Method:

df = df.str.replace('Facility-based testing-OH','Facility based testing-OH')

2nd Method:

df['A'] = df['A'].str.replace(['Facility-based testing-OH'], "Facility based testing-OH"), inplace=True

3rd Method

df.loc[df['A'].isin(['Facility-based testing-OH'])] = 'Facility based testing-OH'

CodePudding user response:

Try:

df["A"] = df["A"].str.replace(
    "Facility-based testing-OH", "Facility based testing-OH", regex=False
)
print(df)

Prints:

                           A
0  Facility based testing-OH
1  Facility based testing-OH

df used:

                           A
0  Facility-based testing-OH
1  Facility based testing-OH
  • Related