Python Forum
Remove \n from list of values within a pandas columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Remove \n from list of values within a pandas columns (/thread-19331.html)



Remove \n from list of values within a pandas columns - klllmmm - Jun-24-2019

I want to replace all \n within pandas dataframe with space.
import pandas as pd
table = pd.DataFrame(data = {'ClientID':[100,102,103],
                             'Category':['A','Category\nZ',['Non\nCategory A','']],
                             'Income':[800,900,[1000,2000]],},)
I tried with the following code. However, I couldn't replace \n within the list values in the columns.

table = table.replace(r'\n',  ' ', regex=True)
Output:
Out[400]: Category ClientID Income 0 A 100 800 1 Category Z 102 900 2 [Non\nCategory A, ] 103 [1000, 2000]
Appreciate if someone can help on this.


RE: Remove \n from list of values within a pandas columns - Clunk_Head - Jun-24-2019

(r'\\n',  ' ', regex=True)
You need to escape the backslash.
Check out regex101.com to test out your regex.


RE: Remove \n from list of values within a pandas columns - klllmmm - Jun-24-2019

(Jun-24-2019, 01:38 AM)Clunk_Head Wrote: (r'\\n',  ' ', regex=True)

Thanks for the reply.

It didn't work as I intended.

table = table.replace(r'\\n',  ' ', regex=True)
Output:
table Out[403]: Category ClientID Income 0 A 100 800 1 Category\nZ 102 900 2 [Non\nCategory A, ] 103 [1000, 2000]