Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...