Pandas:ValueError:无法将浮点 NaN 转换为整数

新手上路,请多包涵

我得到 ValueError: cannot convert float NaN to integer for following:

 df = pandas.read_csv('zoom11.csv')
df[['x']] = df[['x']].astype(int)

  • “x”是 csv 文件中的一列,我无法在文件中发现任何 浮点 NaN ,而且我不明白错误或为什么会得到它。
  • 当我将该列读取为字符串时,它的值如 -1,0,1,…2000,对我来说都是非常好的 int 数字。
  • 当我将该列读取为浮点数时,可以加载它。然后它显示值为 -1.0,0.0 等,仍然没有任何 NaN-s
  • 我尝试在 read_csv 中使用 error_bad_lines = False 和 dtype 参数无济于事。它只是取消加载相同的异常。
  • 该文件不小(10+ M 行),因此无法手动检查它,当我提取一个小标题部分时,没有错误,但它发生在完整文件中。所以它是文件中的东西,但无法检测到什么。
  • 从逻辑上讲,csv 不应该有缺失值,但即使有一些垃圾,我也可以跳过这些行。或者至少识别它们,但我看不到扫描文件并报告转换错误的方法。

更新:使用评论/答案中的提示,我得到了我的数据:

 # x contained NaN
df = df[~df['x'].isnull()]

# Y contained some other garbage, so null check was not enough
df = df[df['y'].str.isnumeric()]

# final conversion now worked
df[['x']] = df[['x']].astype(int)
df[['y']] = df[['y']].astype(int)

原文由 JaakL 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.3k
2 个回答

要识别 NaN 值,请使用 boolean indexing

 print(df[df['x'].isnull()])

然后删除所有非数字值使用 to_numeric 参数 errors='coerce' - 将非数字值替换为 NaN s:

 df['x'] = pd.to_numeric(df['x'], errors='coerce')

并且要删除 NaN 列中的所有行 x 使用 dropna

 df = df.dropna(subset=['x'])

最后将值转换为 int s:

 df['x'] = df['x'].astype(int)

原文由 jezrael 发布,翻译遵循 CC BY-SA 4.0 许可协议

ValueError:无法将 float NaN 转换为整数

从 v0.24 开始,您实际上可以。 Pandas 引入了 可空整数数据类型,它允许整数与 NaN 共存。

给定一系列缺少数据的整数浮点数,

 s = pd.Series([1.0, 2.0, np.nan, 4.0])
s

0    1.0
1    2.0
2    NaN
3    4.0
dtype: float64

s.dtype
# dtype('float64')

您可以将其转换为可为空的 int 类型(选择 Int16Int32Int64 ),–

 s2 = s.astype('Int32') # note the 'I' is uppercase
s2

0      1
1      2
2    NaN
3      4
dtype: Int32

s2.dtype
# Int32Dtype()

您的专栏需要有整数才能进行转换。其他任何事情都会引发 TypeError:

 s = pd.Series([1.1, 2.0, np.nan, 4.0])

s.astype('Int32')
# TypeError: cannot safely cast non-equivalent float64 to int32

原文由 cs95 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题