pandas对于带有基数为10的long()的文字无效 [英] pandas invalid literal for long() with base 10 error

查看:292
本文介绍了pandas对于带有基数为10的long()的文字无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试: df ['Num_Detections'] = df ['Num_Detections']。astype(int)

我得到以下错误:


ValueError:long()的基数为10的无效文字:'12 .0'

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

我的数据看起来如下:

>>> 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 ?

感谢您的帮助。

推荐答案

有一些值,无法转换为 int

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

您可以使用 to_numeric 并获取 NaN 哪里有问题值:

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

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

如果需要检查有问题的行matic值,使用 布尔索引 isnull

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()])

示例:

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

这篇关于pandas对于带有基数为10的long()的文字无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆