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!

martifapa
  • 107
  • 3
  • 12

3 Answers3

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)
emiljoj
  • 399
  • 1
  • 7
  • Hey emiljoj, trying to convert to float (which would work for me) gives me another ValueError: could not convert string to float: '6,92'... – martifapa Apr 11 '20 at 13:10
1

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

df['number'] = df['number'].str.replace(',','').astype(int)
Sajan
  • 1,247
  • 1
  • 5
  • 13
0

You can do the following:-

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

Lovleen Kaur
  • 342
  • 2
  • 9
  • Hey Lovleen Kaur, trying to convert to 'int32' gives me the following a ValueError: invalid literal for int() with base 10: '11,46'! – martifapa Apr 11 '20 at 13:12