Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算列值是否等于NaN或零?_Python_Pandas - Fatal编程技术网

Python 如何计算列值是否等于NaN或零?

Python 如何计算列值是否等于NaN或零?,python,pandas,Python,Pandas,我使用下面的代码来指示列中是否有任何缺失值(NaN)或零(0.00) # Specifying the NaNs num_nan_totals = df.loc[ (pd.isna(df['Totals'])) , 'Totals' ].shape[0] # Specifying the zeros num_zero_totals = df["Totals"] == 0.00 # For output print(f"There are {num_nan_totals} NaNs in th

我使用下面的代码来指示列中是否有任何缺失值(NaN)或零(0.00)

# Specifying the NaNs
num_nan_totals = df.loc[ (pd.isna(df['Totals'])) , 'Totals' ].shape[0]

# Specifying the zeros
num_zero_totals = df["Totals"] == 0.00

# For output
print(f"There are {num_nan_totals} NaNs in the totals column")
print(f"There are {num_zero_totals} zeros in the totals column")
我的输出:

There are 0 NaNs in the totals column
There are 433      False
434      False
435      False
436      False
# etc. etc. etc.

目视检查数据集后,应该至少有一个“0.00”实例,这就是我知道它出错的原因。我怀疑问题出在零的定义上,有人能给出一些提示吗?谢谢

构建遮罩的方法是正确的。假设您只需要计数,您可以使用pandas的
sum
方法。此处的信息:

对于掩码,False为0,True为1,因此将所有值相加是获得所有真值计数的快速方法

# Count of nan
num_nan_totals = df['Totals'].isna().sum()
# Count of 0
num_zero_totals = (df['Totals'] == 0.00).sum()

你在正确的轨道上建造了面具。假设您只需要计数,您可以使用pandas的
sum
方法。此处的信息:

对于掩码,False为0,True为1,因此将所有值相加是获得所有真值计数的快速方法

# Count of nan
num_nan_totals = df['Totals'].isna().sum()
# Count of 0
num_zero_totals = (df['Totals'] == 0.00).sum()

查看
df.info()
谢谢@coldspeed您建议我使用“非空”数字来表示这一点吗?我的值是完整的,所以所有列都是完整的(没有空值),但我希望找到零的实例。也许我错过了什么?是的。您可以从
len(df)
中获得长度,然后您就可以计算出nan的数量。对于零,您需要
(df==0)。求和(1)
。就这些。请查看
df.info()
谢谢@coldspeed您建议我使用“非空”的数字来表示这一点吗?我的值是完整的,所以所有列都是完整的(没有空值),但我希望找到零的实例。也许我错过了什么?是的。您可以从
len(df)
中获得长度,然后您就可以计算出nan的数量。对于零,您需要
(df==0)。求和(1)
。仅此而已。