Berkay Çamur
Berkay Çamur

Reputation: 5

Invalid value error during converting string to int

I have a data set and there is a feature which containing numbers in string like

"153", "45", "13", "345"

I'd like to convert these values to integer with python and i wrote this line of code:

df.column = df.column.astype("int")

But i'm getting this error:

invalid literal for int() with base 10: '2,83E+05'

There is some value like:

3.89E+05, 2.60E+05, 3,13E+05

How can i convert it to any numerical data type? Thanks in advance

enter code here

Upvotes: 0

Views: 929

Answers (1)

AlecZ
AlecZ

Reputation: 589

Problem isn't the scientific notation per se, but the fact that they are float values AND they're in scientific notation. I found that this works as a one line solution:

df.column.astype('float64').astype('int64')

If your string values are in European convention, you can add the following line as well to put in a pandas-friendly format.

df.column = df.apply(lambda x: str(x.column).replace(',','.'), axis=1)
df.column.astype('float64').astype('int64')

Upvotes: 1

Related Questions