检查csv列中的所有项目,除了一个[python pandas] [英] check all items in csv column except one [python pandas]

查看:65
本文介绍了检查csv列中的所有项目,除了一个[python pandas]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用python pandas检查整列以验证所有值都是整数,除了一个以外.一行名称将始终具有浮点数.CSV示例:

I'm trying to figure out how to check an entire column to verify all values are integers, except one, using python pandas. One row name will always have a float num. CSV example:

name, num
random1,2
random2,3
random3,2.89
random4,1
random5,3.45

在此示例中,假设'random3的num始终是浮点数.因此,random5也是浮点数的事实意味着程序应在终端上显示一个错误,告诉用户.

In this example, let's say 'random3's num will always be a float. So that fact that random5 is also a float, means the program should print an error to the terminal telling the user this.

推荐答案

当熊猫 read_csv()函数将CSV文件加载到数据帧中时,会将float dtype分配给包含以下内容的任何列浮点数和整数值.要测试列的元素是否可以精确地表示为整数,可以使用 ="is_integer()的浮点方法,如如何检查"float pandas"列是否仅包含整数?

When the pandas read_csv() function loads the CSV file into a dataframe, it will assign the float dtype to any column that contains float and integer values. To test if the elements of the column can be expressed exactly as integers, you can use the .is_integer() method of floats as described in the answer to How to check if float pandas column contains only integer numbers?

在您的情况下,您要验证列中只有一个浮点数,因此,请执行以下操作:

In your case, you want to verify that you have only one float in the column, so do this:

import pandas as pd
df = pd.DataFrame({'name':[f"random{i}" for i in range(1,6)], 'num':[2, 3, 2.89, 1, 3.45]})

if sum(~df.num.apply(float.is_integer)) != 1:
    print("Error, the data column contains the wrong number of floats!")

如果该列可能仅包含整数,则该列将具有整数dtype,并且上面的代码将导致错误.您可能会发现错误,也可以测试这种情况:

If it is possible that the column only contains integers, then the column will have an integer dtype and the above code will cause an error. You could catch the error, or you could also test for this case:

from pandas.api.types import is_float_dtype

if not is_float_dtype(df.num) or sum(~df.num.apply(float.is_integer)) != 1:
    print("Error, the data column contains the wrong number of floats!")

这篇关于检查csv列中的所有项目,除了一个[python pandas]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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