converting object types columns into numeric type using pandas

回眸只為那壹抹淺笑 提交于 2019-12-24 09:21:01

问题


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')

Then,

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.

Any help will be greatly appreciated.

Thanking in advance.


回答1:


From the help page of to_numeric, the description for errors is as follows:

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

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.

Try using the second option, errors='coerce'.

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

Or,

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

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



来源:https://stackoverflow.com/questions/48279405/converting-object-types-columns-into-numeric-type-using-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!