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'
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', '')
source share