使用pandas将对象类型列转换为数字类型 [英] converting object types columns into numeric type using pandas

查看:962
本文介绍了使用pandas将对象类型列转换为数字类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用熊猫清理数据。当我执行df.datatypes时,它表明列是对象类型。我希望将它们转换为数字类型。
我尝试了各种方式,例如;

I am trying to clean the data using pandas. When I execute df.datatypes it shows that the columns are of type objects. I wish to convert them into numeric types. I tried various ways of doing so like;

data[['a','b']] = data[['a','b']].apply(pd.to_numeric, errors ='ignore')

然后,

data['c'] = data['c'].infer_objects()

但似乎没有任何效果。解释器不会引发任何错误,但同时不会执行所需的转换。

But nothing seems to be working. The interpreter does not throw any error but at the same time, it does not performs the desired conversion.

任何帮助将不胜感激。

预先感谢。

推荐答案

to_numeric ,对于错误的描述如下:

errors : {'ignore', 'raise', 'coerce'}, default 'raise'
        - If 'raise', then invalid parsing will raise an exception
        - If 'coerce', then invalid parsing will be set as NaN
        - If 'ignore', then invalid parsing will return the input

如果您的 apply 返回的输入内容没有执行任何操作,那么原因是因为您有不可转换的对象,并且调用了 to_numeric errors ='ignore'并没有帮助。

If your apply returns your input without doing anything to it, then the reason is because you've non-convertible objects, and calling to_numeric with errors='ignore' isn't helping.

尝试使用第二个选项 errors ='coer ce'

df = df.apply(pd.to_numeric, errors='coerce')

或者,

for c in df.columns:
    df[c] = pd.to_numeric(df[c], errors='coerce')

此外, infer_objects 执行软类型转换。如果要检查列的dtype,请改用 df.dtypes

Also, infer_objects performs soft type-casting. If you want to check column dtypes, use df.dtypes instead.

这篇关于使用pandas将对象类型列转换为数字类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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