Search code examples
pythonpandasspecial-characters

Pandas str.replace of pipe character not working?


I am facing an issue in using pandas str.replace on Series. I am using pandas in Jupyter notebook (although the result is the same with regular python script).

import pandas as pd
s = ["abc | def"]
df = pd.DataFrame(data=s)

print(s[0].replace(" | ", "@"))
print(df[0].str.replace("| ", "@"))
print(df[0].map(lambda v: v.replace("| ", "@")))

Here is the result

ipython Untitled1.py 

abc@def
0    @a@b@c@ @|@ @d@e@f@
Name: 0, dtype: object
0    abc @def
Name: 0, dtype: object

Solution

  • It works if you escape the pipe.

    >>> df[0].str.replace(" \| ", "@")
    0    abc@def
    Name: 0, dtype: object
    

    The str.replace function is equivalent to re.sub:

    import re
    
    >>> re.sub(' | ', '@', "abc | def")
    'abc@|@def'
    
    >>> "abc | def".replace(' | ', '@')
    'abc@def'
    

    Series.str.replace(pat, repl, n=-1, case=True, flags=0): Replace occurrences of pattern/regex in the Series/Index with some other string. Equivalent to str.replace() or re.sub().