I am trying to convert strings to float, but I get the error in the title. I don't understand why it doesn't recognise period ('.') as a decimal. Here is a head of my dataframe.
Country Variable \
0 Afghanistan Inflation, GDP deflator (annual %)
1 Afghanistan GDP (constant 2010 US$)
2 Afghanistan Population, total
3 Afghanistan Population ages 15-64, total
4 Afghanistan Employment to population ratio, 15+, total (%)...
2007 [YR2007] 2008 [YR2008] 2009 [YR2009] 2010 [YR2010] \
0 22.3820157780035 2.17910328500052 -2.10708255443797 9.43779477259656
1 11721187594.2052 12144482858.18 14697331940.6464 15936800636.2487
2 26616792 27294031 28004331 28803167
3 13293041 13602366 13950492 14372378
4 47.1220016479492 47.0480003356934 47.015998840332 47.0429992675781
And here is the code (Python 3.6):
growth_raw.iloc[:,3:] = growth_raw.iloc[:,3:].values.astype('float64')
I get:
ValueError: could not convert string to float: '.'
Any wise thoughts appreciated. Many thanks.
Update: I had accidentally converted NAs '..' to '.'. I have now converted them to ''. I now get
ValueError: could not convert string to float:
I have tried
growth_raw.apply(lambda x: x.str.strip())
For conversion, I have tried
growth_raw.iloc[:,2:].values.astype(float)
Which gives me the above error. I have also tried the following two which give me no error, but do nothing to the data:
growth_raw.iloc[:,2:].apply(lambda x: pd.to_numeric(x), axis=0)
growth_raw.iloc[:,2:].apply(pd.to_numeric,errors='coerce')
0.0might be represented as just.. How do you want to handle that data?pd.to_numeric