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?
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With