gpt4 book ai didi

python - 将对象(字符串)转换为 Int32 : TypeError: object cannot be converted to an IntegerDtype 时出错

转载 作者:行者123 更新时间:2023-12-03 16:00:42 25 4
gpt4 key购买 nike

尝试将 Pandas 中的对象(字符串)列转换为 Int32 时出现以下错误这是允许 NA 的整数类型值。

df.column = df.column.astype('Int32')

TypeError: object cannot be converted to an IntegerDtype



我正在使用 Pandas 版本:0.25.3

最佳答案

这是已知的错误,如 here 所述.
解决方法是先将列转换为 float而不是到 Int32 .
在进行转换之前,请确保从空格中删除您的列:

df.column = df.column.str.strip()
比做转换:
df.column = df.column.astype('float')  # first convert to float before int
df.column = df.column.astype('Int32')
或更简单:
 df.column = df.column.astype('float').astype('Int32') # or Int64

关于python - 将对象(字符串)转换为 Int32 : TypeError: object cannot be converted to an IntegerDtype 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com