Bottom Page

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.
Quote
#2
(r'\\n',  ' ', regex=True)
You need to escape the backslash.
Check out regex101.com to test out your regex.
Quote
#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]
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 98 Yesterday, 04:51 AM
Last Post: pjfarley3
  access dictionary with keys from another and write values to list redminote4dd 6 233 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 379 Apr-10-2020, 02:39 PM
Last Post: buran
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 452 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Do Calculation between Rows based on Column values - Pandas Dataframe ahmedwaqas92 0 362 Jan-28-2020, 07:06 AM
Last Post: ahmedwaqas92
  output a list of random numbers 'x' columns wide adityavpratap 4 272 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Remove all \n from end of list items JackMack118 4 20,083 Dec-27-2019, 08:34 AM
Last Post: perfringo
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 867 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  can't remove element from a list yokaso 6 401 Nov-06-2019, 03:11 PM
Last Post: yokaso
  How do I map a list of values to specified colors? larkypython 4 436 Nov-05-2019, 09:22 AM
Last Post: larkypython

Forum Jump:


Users browsing this thread: 1 Guest(s)