Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting strings to floats: ValueError: could not convert string to float: '.'

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')
like image 691
Minsky Avatar asked Feb 09 '26 19:02

Minsky


1 Answers

Use pd.to_numeric to be on safer side with erros = 'coerce' ( there might be some bad data in real) i.e

df.iloc[:,3:].apply(pd.to_numeric,errors='coerce')
like image 69
Bharath Avatar answered Feb 12 '26 15:02

Bharath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!