Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix invalid literal for int() with base 10 error in pandas

This is the error that is showing up whenever i try to convert the dataframe to int.

("invalid literal for int() with base 10: '260,327,021'", 'occurred at index Population1'

Everything in the df is a number. I assume the error is due to the extra quote at the end but how do i fix it?

like image 829
Caribgirl Avatar asked May 08 '17 23:05

Caribgirl


2 Answers

Others might encounter the following issue, when the string is a float:

    >>> int("34.54545")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '34.54545'

The workaround for this is to convert to a float first and then to an int:

>>> int(float("34.54545"))
34

Or pandas specific:

df.astype(float).astype(int)
like image 151
kristian Avatar answered Oct 01 '22 02:10

kristian


I solved the error using pandas.to_numeric

In your case,

data.Population1 = pd.to_numeric(data.Population1, errors="coerce")

'data' is the parent Object.

After that, you can convert float to int as well

data.Population1.astype(int)
like image 44
Abhishek Sinha Avatar answered Oct 01 '22 02:10

Abhishek Sinha