如何检查float pandas列是否仅包含整数? [英] How to check if float pandas column contains only integer numbers?
本文介绍了如何检查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屋!
查看全文