带有 base 10 错误的 long() 的 pandas 无效文字 [英] pandas invalid literal for long() with base 10 error

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

问题描述

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

我收到以下错误:

<块引用>

ValueError: 以 10 为基数的 long() 的无效文字:'12.0'

我的数据看起来如下:

<预><代码>>>>df['Num_Detections'].head()出[6]:sku_nameDOBRIY MORS 葡萄-蔓越莓-覆盆子 1L 12.0海蓝宝石 5.0L 9.0多布里菠萝 1.5L 2.0FRUKT.SAD 苹果 0.95L 154.0多布里桃苹果 0.33L 71.0名称:Num_Detections,数据类型:对象

知道如何正确进行转换吗?

感谢您的帮助.

解决方案

有一些值,不能转换为int.

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

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

如果需要检查有问题的行,请使用 布尔索引 带掩码 isnull:

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

示例:

df = pd.DataFrame({'Num_Detections':[1,2,'a1']})打印 (df)Num_Detections0 11 22 a1打印 (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])Num_Detections2 a1df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')打印 (df)Num_Detections0 1.01 2.02 南

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.

解决方案

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

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

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