我试图弄清楚如何使用 python pandas 检查整个列以验证所有值都是整数(除了一个)。一行名称始终有一个浮点数。 CSV 示例:
name, num
random1,2
random2,3
random3,2.89
random4,1
random5,3.45
在此示例中,假设“random3”的 num 始终是浮点数。因此,random5 也是一个浮点数,这意味着程序应该向终端打印一条错误,告诉用户这一点。
尝试这个:
if len(df.num.apply(type) == float) >= 2:
print(f"Ups!. There are {len(df.num.apply(type) == float)} float numbers in the column") float numbers in the column")
各成分说明:
df.num.apply(type) # Generates a series showing the amount of rows per class
(df.num.apply(type) == float) # Derived series sorting only the values with the defined class.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)