0

Right now I'm working with a file containing scraped numbers, but whenever I try to do some calculations with them a ValueError (invalid literal for int() with base 10: '6,92') pops up.

The piece of code I use to get the numbers from the web looks like this:

numberX = driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div/div/div[1]/div[2]/div/div/div[3]/span/i').text
number = ''
for i in numberX:
    if i in '0123456789,':
        number += i

'numberX' contains alphanumerical values, but 'number' does not. Still, the dtype of 'number' is object.

I have tried:

df['number'].astype(str).astype(int)

But the same ValueError pops up.

If I export the data to Excel, the column with the 'number' values appears in text format, and Excel gives me the possibility to convert them to numbers. Also, I have checked and the values only contain numbers and a commas (',').

A piece of the printed dataframe looks something like this:

Date  Amount  Number
0   11/04/2020   10000        6,92
1   11/04/2020   10000        6,77
2   11/04/2020   10000        6,66
3   11/04/2020   10000        6,59

Any idea of what could be happening?

Thanks in advance!

3 Answers 3

2

Would converting to floats first instead of int work for you?

df['number'] = df['number'].astype(str).astype(float)

and if you want to convert to int you can still go one extra step:

df['number'] = df['number'].astype(str).astype(float).astype(int)

You could have also come to this solutin through already answered questions: ValueError: invalid literal for int() with base 10: ''

Updated

need to replace the comma with a dot as well:

df['number'].apply(lambda x: x.replace(',','.')).astype(float)
Sign up to request clarification or add additional context in comments.

1 Comment

Hey emiljoj, trying to convert to float (which would work for me) gives me another ValueError: could not convert string to float: '6,92'...
1

I think you may want to replace the , and then convert it to int.

df['number'] = df['number'].str.replace(',','').astype(int)

Comments

0

You can do the following:-

df['number'] = df['number'].astype('int32')

1 Comment

Hey Lovleen Kaur, trying to convert to 'int32' gives me the following a ValueError: invalid literal for int() with base 10: '11,46'!

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.