Skip to content Skip to sidebar Skip to footer

How To Remove Newline In Pandas Dataframe Columns?

I want to shorten and clean up a CSV file to use it in ElasticSearch. but there are line breaks in some Dataframes (cells) and it is not possible to parse the CSV to ElasticSearch.

Solution 1:

From what I've learnt, the third parameter for the .replace() parameter takes the count of the number of times you want to replace the old substring with the new substring, so instead just remove the third parameter since you don't know the number of times the new line exists.

new_f = f[keep_col].replace('\\n',' ')

This should help

Solution 2:

In case, using pandas data-frame is not compulsory , you can do it in following way using simple python:

withopen('test.csv', 'r') as txtReader:
    withopen('new_test.csv', 'w') as txtWriter:
        for line in txtReader.readlines():
            line = line.replace('\\n', '')
            txtWriter.write(line)

Post a Comment for "How To Remove Newline In Pandas Dataframe Columns?"