0

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

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

4 Comments

it says 'could not convert string to float: '2,83E+05' ' I actually tried a thing that similar to that.
See the update, should work with your number conventions
I dropped all NA values but there is still "Cannot convert non-finite values (NA or inf) to integer" error.
You can filter infinite values (or NA, or other specific) with numpy: df = df.column.replace({numpy.inf:0, numpy.nan:0})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.