2

I'm trying to replace multiple characters with one at the same time, but I can't

df = pandas.DataFrame({"Col1":["A", "B", "ABC", "D", "AB"], "Col2":["H", "I", "J", "K", "L"]})

>>> df
       Col1       Col2
0         A          H
1         B          I
2       ABC          J
3         D          K
4        AB          L

Replace multiple characters of a column in a dataframe

df['Col1'] = df['Col1'].str.replace(['C','D'],'W')

>>>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Laura\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\strings.py", line 1954, in wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\Laura\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\strings.py", line 2777, in replace
    self._parent, pat, repl, n=n, case=case, flags=flags, regex=regex
  File "C:\Users\Laura\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\strings.py", line 713, in str_replace
    compiled = re.compile(pat, flags=flags)
  File "C:\Users\Laura\AppData\Local\Programs\Python\Python37-32\lib\re.py", line 234, in compile
    return _compile(pattern, flags)
  File "C:\Users\Laura\AppData\Local\Programs\Python\Python37-32\lib\re.py", line 276, in _compile
    return _cache[type(pattern), pattern, flags]
TypeError: unhashable type: 'list'

Separately if it works, but I think it is not the best way to do it

df['Col1'] = df['Col1'].str.replace('C','W')
df['Col1'] = df['Col1'].str.replace('D','W')

>>> df
       Col1       Col2
0         A          H
1         B          I
2       ABW          J
3         W          K
4        AB          L

How can I do it at the same time or in a more efficient way?

1 Answer 1

3

The string in str.replace is regex pattern, so you can take advantage of it:

df['Col1'] = df['Col1'].str.replace(r'C|D','W')

print(df)

Prints:

  Col1 Col2
0    A    H
1    B    I
2  ABW    J
3    W    K
4   AB    L
Sign up to request clarification or add additional context in comments.

1 Comment

What if we want to replace multiple characters for different characters each?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.