Pandas df to csv inserts blank lines

Viewed 4561

I have a pandas data frame 'data' that I want to export to csv. It works fine, but some lines are completely messed up. I use: data.to_csv('data.csv')

I tried altering the encoding and dropping index, but it didn't work. The first picture are the correct rows, and the second one the rows that got messed up. When looking at the messed up rows i figured the problem might be that the text data uses the delimiter \r to separate lines and is exported incorrectly. How can I fix this?

what it looks like for correct rows

what the incorrect rows look like

Thanks!

2 Answers

Just change the line terminator:

data.to_csv('data.csv',line_terminator = '\r')

Change the separator to \t or | or something other than ,. You have commas in your strings, which are messing up your comma separation.

Note that your csv's are nothing but text files separating elements using commas. If you have commas in uneven places, it's gonna make extra separation and hence, going to mess up your file.

Related