【问题标题】:how to convert column of object datatype to int64 datatype in python pandas如何在python pandas中将对象数据类型的列转换为int64数据类型
【发布时间】:2019-10-11 03:36:21
【问题描述】:

我合并了两个表,因此一列有一个 dtype 对象,我需要将其转换为 int,我在其中遇到类型错误。

我使用了astype函数,但是对象没有转换成string,得到如下类型错误:

TypeError: int() 参数必须是字符串、类似字节的对象或 数字,而不是“方法”

【问题讨论】:

  • df['col_name'].astype(int) 而不是df['col_name'].astype(int())
  • 使用上面的一段代码,还是一样的错误

标签: python pandas type-conversion


【解决方案1】:

我认为在您的数据框中您有一些 NaN 值,请先使用

进行检查
df.isnull().sum()

然后填充或删除 NaN 值,然后您可以转换您的值。

如果不是问题,你可以尝试类似的方法吗:

df['col']=df['col'].apply(lambda x : int(x))

【讨论】:

  • 没有空值,我已经检查并填写了这些值
  • 我尝试了上面的代码,to_numeric 但没有任何帮助
猜你喜欢
  • 2022-12-30
  • 2020-12-18
  • 1970-01-01
  • 2017-02-03
  • 2018-11-06
  • 2019-08-04
  • 2018-05-05
  • 2018-11-08
  • 2012-09-20
相关资源
最近更新 更多