我使用以下代码来指示列中是否有任何缺失值 (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"实例,这就是我知道它出错的原因。我怀疑问题出在零定义上,任何人都可以给出任何提示吗?谢谢!
你在构建面具方面走在正确的轨道上。假设你只需要计数,你可以使用熊猫的sum
方法。信息在这里: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html
对于掩码,False 为 0,True 为 1,因此将所有值相加是获取所有 true 值计数的快速方法。
# Count of nan
num_nan_totals = df['Totals'].isna().sum()
# Count of 0
num_zero_totals = (df['Totals'] == 0.00).sum()