Laura MS
Laura MS

Reputation: 101

Replace multiple characters of a column in a dataframe con python

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?

Upvotes: 2

Views: 2611

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

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

Upvotes: 3

Related Questions