如何检查float pandas列是否仅包含整数? [英] How to check if float pandas column contains only integer numbers?

查看:440
本文介绍了如何检查float pandas列是否仅包含整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框

df = pd.DataFrame(data=np.arange(10),columns=['v']).astype(float)

如何确保v中的数字是整数? 我非常担心舍入/截断/浮点表示错误

How to make sure that the numbers in v are whole numbers? I am very concerned about rounding/truncation/floating point representation errors

推荐答案

astype(int)

的比较

暂时将您的列转换为int并使用np.array_equal进行测试:

Comparison with astype(int)

Tentatively convert your column to int and test with np.array_equal:

np.array_equal(df.v, df.v.astype(int))
True


float.is_integer

您可以将此Python函数与apply结合使用:


float.is_integer

You can use this python function in conjunction with an apply:

df.v.apply(float.is_integer).all()
True

或者,在生成器理解中使用python的all来提高空间效率:

Or, using python's all in a generator comprehension, for space efficiency:

all(x.is_integer() for x in df.v)
True

这篇关于如何检查float pandas列是否仅包含整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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