Pandas Dataframe Removing New Line Feeds

I have the following data:

          all_data
0     \n\n\n\n\n\n\n\n
1      \n\n\n\n\n\n\n\n\n\n\n\n

How can I remove the '\n' characters?

I have tried the following:

df.replace(r'\\n','',regex=True)
df = df.replace(r'\\n','',regex=True)

What am I doing wrong?

Upvotes: 0

Views: 49

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Use 1 or more regex expression.

df.replace(r'\\n+', '', regex=True)

Output:

  all_data
0         
1         

Upvotes: 2

Mayank Porwal
Mayank Porwal

Reputation: 34046

Do this:

In [3067]: df['all_data'].str.replace(r'\\n','',regex=True) 
Out[3067]: 
0    
1    
Name: all_data, dtype: object

OR

In [3068]: df.apply(lambda x: x.str.replace(r'\\n',''))
Out[3068]: 
  all_data
0         
1       

Upvotes: 2

Related Questions