cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Removing new line character from spark dataframe column

ShriS1221
New Contributor II

I have to remove new line character from entire column of a dataframe , I tried with regex_replace but its not working.

Help me on this.​

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz
Community Manager
Community Manager

Hi @Shriram S​ ,

Please try the following code:

import pandas as pd
 
df=pd.read_csv("xyz.csv")
 
col = ["columns_of _the _dataframes_that_needs_modification"]
 
new_df = df[col].replace('\\n',' ', regex=True)
 
new_df.to_csv("newFile.csv", index=False)

View solution in original post

5 REPLIES 5

Kaniz
Community Manager
Community Manager

Hi @Shriram S​ ! My name is Kaniz, and I'm the technical moderator here. Great to meet you, and thanks for your question! Let's see if your peers in the community have an answer to your question first. Or else I will get back to you soon. Thanks.

ShriS1221
New Contributor II

Yeah , thank you .​

AmanSehgal
Honored Contributor III

Could you please provide example of your data and the code you've tried?

Kaniz
Community Manager
Community Manager

Hi @Shriram S​ ,

Please try the following code:

import pandas as pd
 
df=pd.read_csv("xyz.csv")
 
col = ["columns_of _the _dataframes_that_needs_modification"]
 
new_df = df[col].replace('\\n',' ', regex=True)
 
new_df.to_csv("newFile.csv", index=False)

Kaniz
Community Manager
Community Manager

Hi @Shriram S​ , Did the above provided code work for you?