Python如何将Series type:object转换为int [英] Python How to convert Series type : object to int

查看:4415
本文介绍了Python如何将Series type:object转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Series对象转换为整数.但是我在做这件事上遇到了麻烦.每次尝试执行某些操作时,我都会遇到新的错误.

I'm trying to convert a Series object to integer. But I'm having trouble doing it. Every time I try something I have a new Error.

  1. 我尝试使用pd.to_numeric进行转换,解析字符串None
  2. 时出错
  3. 然后我尝试用NaN替换None值:替换时出现问题
  1. I tried to convert using pd.to_numeric, error while parsing string None
  2. Then I tried to replace None values with NaN : problem replacing

#1.1)

pd.to_numeric(df['Var1'], downcast = 'integer')


ValueError: Unable to parse string "None" at position 44816

#1.2)

df.astype({'Var1':'int64'}).dtypes


TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

#2)

df['Var1'].astype(str).astype(int)


ValueError: invalid literal for int() with base 10: 'None'

实际结果:dtype:object
预期结果:dtype:int64

actual result: dtype: object
expected result: dtype: int64

推荐答案

尝试

import numpy as np
df = df.fillna(np.nan)

这篇关于Python如何将Series type:object转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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