Pandas carriage return in to_csv

I have a string column that sometimes returns carriages in a row:

import pandas as pd from io import StringIO datastring = StringIO("""\ country metric 2011 2012 USA GDP 7 4 USA Pop. 2 3 GB GDP 8 7 """) df = pd.read_table(datastring, sep='\s\s+') df.metric = df.metric + '\r' # append carriage return print(df) country metric 2011 2012 0 USA GDP\r 7 4 1 USA Pop.\r 2 3 2 GB GDP\r 8 7 

When writing and reading from csv, the framework gets damaged:

 df.to_csv('data.csv', index=None) print(pd.read_csv('data.csv')) country metric 2011 2012 0 USA GDP NaN NaN 1 NaN 7 4 NaN 2 USA Pop. NaN NaN 3 NaN 2 3 NaN 4 GB GDP NaN NaN 5 NaN 8 7 NaN 

Question

What is the best way to fix this? One obvious method is to simply clear the data first.

 df.metric = df.metric.str.replace('\r', '') 
+5
source share
1 answer

Specify lineterminator :

 print(pd.read_csv('data.csv', lineterminator='\n')) country metric 2011 2012 0 USA GDP\r 7 4 1 USA Pop.\r 2 3 2 GB GDP\r 8 7 
+5
source

Source: https://habr.com/ru/post/1239593/


All Articles