Python Forum

Full Version: Remove \n from list of values within a pandas columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
(r'\\n',  ' ', regex=True)
You need to escape the backslash.
Check out regex101.com to test out your regex.
(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]