ASH
ASH

Reputation: 20302

Can't Convert Object to Float. What's the Best Workaround?

I'm struggling to convert an object to a float.

df_final['INBCS'] = df_final['INBCS'].astype(float)

It keeps saying: ValueError: could not convert string to float: '1,620,000'

If I try a different approace, I get mostly NAN results.

print(pd.to_numeric(df_final['INBCS'], errors='coerce'))

I tried one more approach, and I still get errors.

df_final = df_final[df_final['INBCS'].apply(lambda x: x.isnumeric())]

There are no NANs in the data; I already converted them to zeros. When I print the data, it shows commas, but there are no commas at all. I even did ran a replace function to get rid of any potential commas, but again, there are no commas in the data. Any idea what's wrong here? Thanks.

Upvotes: 0

Views: 1403

Answers (3)

sophocles
sophocles

Reputation: 13821

The reason is because you have , there, you can do:

df_final['INBCS'] = df_final['INBCS'].replace(',','')
df_final['INBCS'] = df_final['INBCS'].astype(float)

should work.

Upvotes: 1

Ken Kinder
Ken Kinder

Reputation: 13140

The reason you can't convert that string to a float is that Python doesn't know what to do with the commas. You can reproduce this easily:

>>> float('1,000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1,000'

It's tempting to just remove the commas and parse the number, but there's an internationalization concern. In some countres, a comma separates thousands (eg, "1,000,000" is one million). In other countries, commas separate decimals (eg, "1,05" is one and five one-hundredths).

For that reason, it's best to use localization to parse a number like that if you can't get it in a native form. See this answer for details on that.

Upvotes: 3

M-Chen-3
M-Chen-3

Reputation: 2054

Try this:

string = '1,620,000'

decimal = float(''.join(string.split(',')))

print(type(decimal), decimal)
# Prints (<type 'float'>, 1620000.0)

This first gets rid of all the commas using split(','), then recreates the string using ''.join(). Finally, it converts the whole thing to a float using float().

Upvotes: 0

Related Questions