sprogissd
sprogissd

Reputation: 3075

How to check if Pandas value is null or zero using Python

I have a data frame created with Pandas that contains numbers. I need to check if the values that I extract from this data frame are nulls or zeros. So I am trying the following:

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

While it does appear to work, sometimes I get the following error:

TypeError: don't know how to convert scalar number to float

What am I doing wrong?

Upvotes: 1

Views: 6322

Answers (1)

Geetha Ponnusamy
Geetha Ponnusamy

Reputation: 497

your code to extract a single value from a series will return list of list format with a single value:

For Example: [[1]]

so try changing your code

 a = df.ix[[0], ['Column Title']].values 

to

 a = df.ix[0, 'Column Title']

then try

math.isnan(float(a))

this will work!!

Upvotes: 6

Related Questions