[英]How can I count if column value is equal to NaN or zero?
I'm using the code below to indicate whether there are any missing values (NaN) or zeros (0.00) in a column. 我正在使用下面的代码来指示一列中是否有任何缺失值(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")
My output: 我的输出:
There are 0 NaNs in the totals column
There are 433 False
434 False
435 False
436 False
# etc. etc. etc.
Having visually checked the dataset, there should be at least one '0.00' instance, which is how I know it's going wrong. 目视检查数据集后,应该至少有一个“ 0.00”实例,这就是我知道它出问题的方式。 I suspect the issue is with the zeros definition, can anyone give any hints?
我怀疑问题与零定义有关,有人可以提供任何提示吗? Thanks!
谢谢!
You are on the right track with building the masks. 您正在建立面具,走上正确的道路。 Assuming, you only want the counts, you can use the
sum
method from pandas. 假设只需要计数,则可以使用熊猫的
sum
方法。 Info here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html 此处的信息: https : //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html
For the mask, False is 0 and True is 1 so adding up all values is a quick way to get the count of all true values. 对于掩码,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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.