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
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove some columns James_S 4 726 Dec-16-2023, 11:02 PM
Last Post: James_S
  Copying the order of another list with identical values gohanhango 7 1,062 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,515 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Remove values for weekend in a panda series JaneTan 0 646 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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