0

Is there a way to get a case insensitive regex replace to work on a Pandas dataframe? I would prefer to keep the work vectorised instead of having to resort to creating a loop at the string level to which re.sub is apparently limited.)

(When I try to use re.sub with the dataframe the compiler complains that it needs strings.)

fables['animal_names'] = re.sub(r"(^|\s+|,)hippo(,|\s+|$)"," Hippopotamus ", fables['animal_names'])

TypeError: expected string or bytes-like object

Community
  • 1
  • 1
leerssej
  • 14,260
  • 6
  • 48
  • 57
  • 1
    Passing `case=True`? – Moses Koledoye Nov 14 '16 at 19:36
  • 1
    Does the regular string `.str` accessor [`str.replace`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html) not work? It has a `case` parameter that can be set to `False`... – root Nov 14 '16 at 19:36
  • 1
    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html how about this method (works on Series) – Oleg Nov 14 '16 at 19:36
  • 1
    or this one : http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html Using `regex=True` – bunji Nov 14 '16 at 19:38
  • So then str.replace does take ignorecase arguments! Adding all of the above works. – leerssej Nov 14 '16 at 19:41

0 Answers0