MMWE:
In [1]: df = pd.DataFrame(
{'A': [0, 1, 2],
'B': ['ba\nt', 'foo', 'bait'],
'C': ['abc', 'ba\nr', 'xyz']}
)
In [2]: df
Out[2]: A B C
0 0 ba\nt abc
1 1 foo ba\nr
2 2 bait xyz
In [3]: df.replace(regex={'\n': '', 'foo': 'xyz'}) # Neither do r'\n' or '\\n' work.
Out[3]: A B C
0 0 ba\nt abc
1 1 xyz ba\nr
2 2 bait xyz
Note that the MMWE is adapted from pandas docs on replace, and I can confirm that the example given in the docs works perfectly, provided the df
doesn't have escaped characters.
Further note, answers using regex to this SO question also do not work.
Expected:
Out[3]: A B C
0 0 bat abc
1 1 xyz bar
2 2 bait xyz
Working:
In [4]: df.replace('\n', '', regex=True).replace('foo', 'xyz')
Out[4]: A B C
0 0 bat abc
1 1 xyz bar
2 2 bait xyz
But, of course, that is not what I am looking for. Should I report this as a bug?
EDIT
Mystery deepens:
In [5]: df.replace(regex={'\n': '', r'^fo.$': 'xyz'})
Out[5]: A B C
0 0 ba\nt abc
1 1 xyz ba\nr
2 2 bait xyz
It seems the issue is only with escaped characters.
EDIT
Version information:
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.24.1