Python Forum
Remove \n from list of values within a pandas columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove \n from list of values within a pandas columns
#1
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.
Reply
#2
(r'\\n',  ' ', regex=True)
You need to escape the backslash.
Check out regex101.com to test out your regex.
Reply
#3
(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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove double quotes from the list ? PythonDev 22 673 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Remove specific elements from list with a pattern Xalagy 3 341 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  convert list to five columns dataframe in sequence tonycat 2 225 Sep-29-2020, 06:47 AM
Last Post: tonycat
  How to remove dict from a list? Denial 7 277 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Trouble with converting list , dict to int values! faryad13 7 431 Sep-04-2020, 06:25 AM
Last Post: faryad13
  How to make a list of values from a dictionary list? faryad13 2 252 Sep-03-2020, 03:45 PM
Last Post: faryad13
  Print variable values from a list of variables xnightwingx 3 343 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Accessing values in list of dictionaries pythonnewbie138 2 266 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 343 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  access dictionary with keys from another and write values to list redminote4dd 6 567 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020