You can use replace
and pass the strings to find/replace as dictionary keys/items:
df.replace({'\n': '<br>'}, regex=True)
For example:
>>> df = pd.DataFrame({'a': ['1\n', '2\n', '3'], 'b': ['4\n', '5', '6\n']})
>>> df
a b
0 1\n 4\n
1 2\n 5
2 3 6\n
>>> df.replace({'\n': '<br>'}, regex=True)
a b
0 1<br> 4<br>
1 2<br> 5
2 3 6<br>
Note that this method returns a new DataFrame instance by default (it does not modify the original), so you’ll need to either reassign the output:
df = df.replace({'\n': '<br>'}, regex=True)
or specify inplace=True
:
df.replace({'\n': '<br>'}, regex=True, inplace=True)