如何使用Python检查Pandas值是否为空或零

spr*_*ssd 1 python pandas

我有一个用熊猫创建的数据框,其中包含数字。我需要检查从此数据框中提取的值是否为空或零。因此,我尝试以下操作:

a = df.ix[[0], ['Column Title']].values  
if a != 0 or not math.isnan(float(a)):
    print "It is neither a zero nor null"
Run Code Online (Sandbox Code Playgroud)

虽然它确实起作用,但有时会出现以下错误:

TypeError: don't know how to convert scalar number to float
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 5

您要从系列中提取单个值的代码将返回具有单个值的列表格式列表:

例如:[[1]]

所以尝试更改您的代码

 a = df.ix[[0], ['Column Title']].values 
Run Code Online (Sandbox Code Playgroud)

 a = df.ix[0, 'Column Title']
Run Code Online (Sandbox Code Playgroud)

然后尝试

math.isnan(float(a))
Run Code Online (Sandbox Code Playgroud)

这会工作!