Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas invalid literal for long() with base 10 error

I am trying to do: df['Num_Detections'] = df['Num_Detections'].astype(int)

And i get following error:

ValueError: invalid literal for long() with base 10: '12.0'

My data looks looks following:

>>> df['Num_Detections'].head()
Out[6]: 
sku_name
DOBRIY MORS GRAPE-CRANBERRY-RASBERRY 1L     12.0
AQUAMINERALE 5.0L                            9.0
DOBRIY PINEAPPLE 1.5L                        2.0
FRUKT.SAD APPLE 0.95L                      154.0
DOBRIY PEACH-APPLE 0.33L                    71.0
Name: Num_Detections, dtype: object

Any idea how to do the conversion correctly ?

Thanks for help.

like image 514
Night Walker Avatar asked Aug 12 '16 13:08

Night Walker


People also ask

How do I fix invalid literal in Python?

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

What is ValueError invalid literal for int () with base 10?

The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. To solve this problem, you can use the float() method to convert a floating-point number in a string to an integer.

How do I change items to numeric in pandas?

to_numeric() The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric(). This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do you change an object to an int in Python?

Convert Column to int (Integer)Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.


1 Answers

There is some value, which cannot be converted to int.

You can use to_numeric and get NaN where is problematic value:

df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')

If need check rows with problematic values, use boolean indexing with mask with isnull:

print (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])

Sample:

df = pd.DataFrame({'Num_Detections':[1,2,'a1']})

print (df)
  Num_Detections
0              1
1              2
2             a1

print (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])
  Num_Detections
2             a1

df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')
print (df)
   Num_Detections
0             1.0
1             2.0
2             NaN
like image 78
jezrael Avatar answered Oct 08 '22 12:10

jezrael